Values stays the same after updating a struct arra

2019-09-06 22:26发布

问题:

I have a file which contains 2^32 key/value pairs like that;

32410806935257552 7355088504912337885
32422108164434515 8864339902215816897
32476145725020530 3183682744179377405
32554129507725755 7487866067392975840
32556703862039701 6580190515895793022
32576110112978588 468697310917255961
32589559935917707 757063057981860288
32724197203660231 4397507527199428746
32740607750878547 497049298362389181
32804063187658851 690408619066859126
....

I need to read that file and get the 1000 lines every time I need.

I am using the function below for this;

void setChunk(pair* pairs, int setNumber, FILE *file){
    int start = setNumber * 1000;
    int finish = start + 1000; 
    int count = 0;
    int i = 0;
    char line [c];
    char *search = " ";

    printf("chunk is set between %d and %d\n", start, finish);
    if ( file != NULL ){

        while (fgets(line, sizeof line, file) != NULL) /* read a line */
        {
            if (count >= start && count < finish)
            {  
                pairs[i].key = strdup(strtok(line, search));

                pairs[i].value = strdup(strtok(NULL, search));

                i++;
            }

            count++;
        }   
    }
}

I am taking the first 1000 thousands key/value pair and write into the struct array (pairs) with no problem. Then I try to get next one thousands pairs but, the key/value struct array stays the same. I couldn't update its content. What can be the reason for this?

note: setNumber defines the which one thousands pairs I will take. If it is 0, I get the lines between 0-1000, if it is 18, I get the lines 18000-19000.

I'm calling setChunk funtion in a loop like this;

for(j=0; j<=fileNumber; j++){
        if ( file[j] != NULL )
        {
            printf("%d. chunks is started to fill\n", j);
            pairs[j] = malloc(1000 * sizeof(pair));
            setChunk(pairs[j],setnumber[j],file[j]);
            setnumber[j]+=1;
        }
    }

回答1:

regarding these lines:

setChunk(pairs[j],setnumber[j],file[j]); 
setnumber[j]+=1;`  

the setnumber is an array,

(lets assume it is initialized to all 0s.)

So entry setnumber[0] is passed on the first call. then setnumber[0] is incremented to 1

On the next call setnumber[1] is passed on the call, then setnumber [1] is incremented to 1.

I.E. on every call the second parameter is always 0.



标签: c arrays struct