简体   繁体   English

fscanf 使用 while 循环从文件中读取

[英]fscanf reading from file with while loop

i am very grateful about all you invloving in answer my question.我非常感谢您回答我的问题。 i found that this bug is caused by wrong operation of pointer.我发现这个错误是由于指针操作错误引起的。 i have fixed it.我已经修好了。

file to read:要读取的文件:

 L 0,1
 L 1,1
 L 2,1
 L 3,1
 S 4,1
 L 5,1
 S 6,1
 L 7,1
 S 8,1
 L 9,1
 S a,1
 L b,1
 S c,1
 L d,1
 S e,1
 M f,1

each line startswith one whitespace end with '\n'.每行以一个空格开头,以 '\n' 结尾。

i have write those code to read lines from file and calling helper(cacheSimulator) func: function cacheSimulator is the starting point of open the file我已经编写了这些代码来从文件中读取行并调用 helper(cacheSimulator) func: function cacheSimulator是打开文件的起点

cacheSimulator as follow cacheSimulator如下

resStruct cacheSimulator(addrStruct* Arg, resStruct res){
    resStruct *pRes = &res;
    
    // fscanf vars
    char op;
    unsigned long addr;
    int bytes;

    int cacheSize = Arg->set * Arg->line * sizeof(unit);
    unit *pCache = malloc(cacheSize);
    if (!pCache) exit(1);
    
    int block = 0, set = 0, tag = 0,
        line = Arg->line; // this var save result in decimal.

    int64_t maskCO, maskCI, maskCT; // address mask
    maskOption optionCO = { Arg->block, 0},\
               optionCI = { Arg->set, Arg->block },\
               optionCT = { ADDRLEN - Arg->block - Arg->set,\
                            Arg->block + Arg->set };
    int r = 0;
    unsigned long time = 0;
    unsigned long *ptime = &time;
    char resChar[MAXRES] = {};
    FILE* pFile = fopen(Arg->File,"r");

    while (true) {
        r = fscanf(pFile, " %c %lx,%d", &op, &addr, &bytes);
        if (r!=3) break;
        block = GetDecimalBit(addr, optionCO);
        set = GetDecimalBit(addr, optionCI);
        tag = GetDecimalBit(addr, optionCT);
        
        memset(resChar, 0, sizeof(resChar));
        if (op == 'M') {
            LoadCache(tag, set, block, line, pCache, pRes, ptime, resChar);
            strcat(resChar, " hit");
            pRes->hits++;
        } else if (op == 'L' || op == 'S') {
             LoadCache(tag, set, block, line, pCache, pRes, ptime, resChar);
          }

        if (Arg->verbose) {
            printf("%c %lx,%d %s\n", op, addr, bytes, resChar);
        }
    }   
    fclose(pFile);
    free(pCache);
    return res;
}

the function LoadCache access a 2D array and deal with a output parameter resChar . function LoadCache访问二维数组并处理 output 参数resChar LoadCache

void LoadCache(int tag,
               int set,
               int block,
               int line,
               unit* pCache,
               resStruct* pRes,
               unsigned long *time,
               char ret[] ) {
    int get = 0;
    unit *pTarget = NULL;
    while (!get) {
        for (int l = 0; l < line; ++l) {
            pTarget = pCache + (set*line+l)*sizeof(unit);
            if (pTarget->valid && pTarget->tag == tag){
                (pRes->hits)++;
                pTarget->timer = *time;
                get = 1;
                strcpy(ret, "hit");
                break;
            } else if (!pTarget->valid) {
                (pRes->misses)++;
                pTarget->timer = *time;
                pTarget->valid = 1;
                pTarget->tag = tag;
                get = 1;
                strcpy(ret, "miss");
                break;
            }
        }
        if (!get) {
            WriteCache(tag, set, block, line, pCache, pRes, time);
            (pRes->misses)++;
            strcpy(ret, "miss eviction");
            get = 1;
        }
    }
    ++*time;

}

but i got this output:但我得到了这个 output:

L 0,1 miss
L 1,1 hit
L 2,1 miss eviction
L 3,1 hit
S 4,1 miss eviction
L 5,1 hit
S 6,1 miss eviction
L 7,1 hit
S 8,1 miss eviction

compared to the file what i fopen, it just read the first 9 lines.与我打开的文件相比,它只读取了前 9 行。 i have gdb the excutable obj.我有 gdb 可执行对象。 i found that when execute the while loop to read the line 10, fscanf would return 1, but 3 what is expected.我发现当执行 while 循环读取第 10 行时,fscanf 将返回 1,但 3 是预期的。

the gdb output: gdb output:

Breakpoint 2, cacheSimulator (Arg=0x7fffffffda80, res=...) at helper.c:88
88          r = fscanf(pFile, " %c %lx,%d", &op, &addr, &bytes);
1: r = 3
(gdb) c
Continuing.
L 7,1 hit

Breakpoint 2, cacheSimulator (Arg=0x7fffffffda80, res=...) at helper.c:88
88          r = fscanf(pFile, " %c %lx,%d", &op, &addr, &bytes);
1: r = 3
(gdb) c
Continuing.
S 8,1 miss eviction

Breakpoint 2, cacheSimulator (Arg=0x7fffffffda80, res=...) at helper.c:88
88          r = fscanf(pFile, " %c %lx,%d", &op, &addr, &bytes);
1: r = 3
(gdb) n
89          if (r!=3) break;
1: r = 1

as you can see, there is no problem reading the first 9 lines.如您所见,阅读前 9 行没有问题。 but return value from fscanf() is 1 when the 10th line is read.但是当读取第 10 行时, fscanf()的返回值为 1。


tried executing the code, got segmentation fault.尝试执行代码,出现分段错误。 replaced "open($filepath,"r");"替换“打开($filepath,”r“);” with "fopen($filepath,"r");"用 "fopen($filepath,"r");" and after this code got executed successfully, and didn't with encounter any error aftr that.并且在此代码成功执行后,之后没有遇到任何错误。

    #include <stdio.h>
    char * filepath = "/Users/test/temp.txt";
    int main () {
        char op=0;
        unsigned long addr=0;
        int bytes=0;
        FILE* pFile = fopen(filepath,"r");
        if (!pFile) {
             printf("Failed to open file\n");
             return 0;
        }
        while (fscanf(pFile, " %c %lx,%d\n", &op, &addr, &bytes) == 3) {
            printf(" %c , %lx %d\n", op, addr, bytes);
            //op = 0;
            //addr=0;
            //bytes=0;
        }
        return 0;

    }

output: output:

    NISM-M-9168:dirtest nism$ cc fileread.c
    NISM-M-9168:dirtest nism$ ./a.out 
     L , 0 1
     L , 1 1
     L , 2 1
     L , 3 1
     S , 4 1
     L , 5 1
     S , 6 1
     L , 7 1
     S , 8 1
     L , 9 1
     S , a 1
     L , b 1
     S , c 1
     L , d 1
     S , e 1
     M , f 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM