值保持不变更新结构数组值后 [英] Values stays the same after updating a struct array value

查看:120
本文介绍了值保持不变更新结构数组值后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文件 2 ^ 32 键/值对像;

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

我需要读取该文件,并获得 1000行每次我需要时间。

我使用了以下功能本;

 无效setChunk(双*双,INT setNumber,FILE *文件){
    INT开始= setNumber * 1000;
    INT完成=开始+ 1000;
    诠释计数= 0;
    INT I = 0;
    焦线[C];
    字符*搜索=;    的printf(块设置%d到%d个\\ n之间,启动完成);
    如果(文件!= NULL){        而(与fgets(行,sizeof的线,档案)!= NULL)/ *读取一行* /
        {
            如果(计数> =&开始放;&安培; COUNT<完成)
            {
                对[I] =。关键的strdup(strtok的(行,搜索));                对[I]。价值=的strdup(的strtok(NULL,搜索));                我++;
            }            算上++;
        }
    }
}

这是我在第1000数以千计的key / value 对,并写入到结构阵列(对)与没问题。然后我试图让下一个几千双,但键/值结构数组保持不变。我无法更新其内容。可能是什么原因呢?

请注意: setNumber 定义哪一个几千双我会抓住。如果是 0 ,我得到 0-1000 之间的界限,如果是 18 ,我得到的线 18000-19000

我在这样的循环中调用setChunk funtion;

 为(J = 0; J< = fileNumber; J ++){
        如果(文件[J]!= NULL)
        {
            的printf(%d个块开始填\\ n,J);
            对[J] =的malloc(1000 * sizeof的(对));
            setChunk(对[J],setnumber [J],文件[J]);
            setnumber [J] + = 1;
        }
    }


解决方案

对于这些行:

  setChunk(对[J],setnumber [J],文件[J]);
setnumber [J] + = 1;`

setnumber 是一个数组,

(让我们假设它被初始化为全0)。

所以进入setnumber [0]传递的第一个电话。然后setnumber [0]被递增到1

在下次调用setnumber [1]在电话会议上通过,那么setnumber [1]递增到1。

即。在每次调用的第二个参数始终为0。

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;
        }
    }

解决方案

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.

这篇关于值保持不变更新结构数组值后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆