[英]Values stays the same after updating a struct array value
I have a file which contains 2^32
key/value pairs like that; 我有一个包含
2^32
键/值对的文件;
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. 我需要读取该文件,并在每次需要时获得
1000 lines
。
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. 我正在使用
first 1000 thousands key/value
对,并毫无问题地写入struct array (pairs)
。 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. 注意:
setNumber
定义了我将采用的千对。 If it is 0
, I get the lines between 0-1000
, if it is 18
, I get the lines 18000-19000
. 如果为
0
,则得到0-1000
之间的线,如果为18
,则得到18000-19000
。
I'm calling setChunk funtion in a loop like this; 我在这样的循环中调用setChunk函数;
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;
}
}
regarding these lines: 关于这些行:
setChunk(pairs[j],setnumber[j],file[j]);
setnumber[j]+=1;`
the setnumber
is an array, setnumber
是一个数组,
(lets assume it is initialized to all 0s.) (假设它已初始化为全0。)
So entry setnumber[0] is passed on the first call. 因此,条目setnumber [0]在第一个调用中传递。 then setnumber[0] is incremented to 1
然后将setnumber [0]递增为1
On the next call setnumber[1] is passed on the call, then setnumber [1] is incremented to 1. 在下一个呼叫上,将setnumber [1]传递给呼叫,然后将setnumber [1]递增到1。
IE on every call the second parameter is always 0. IE在每次调用时的第二个参数始终为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.