Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用while循环从文件读取fscanf 我非常感谢你们回答我的问题。 我发现这个错误是由指针的错误操作引起的。我已经修好了。_C_String_File_Printf_Scanf - Fatal编程技术网

使用while循环从文件读取fscanf 我非常感谢你们回答我的问题。 我发现这个错误是由指针的错误操作引起的。我已经修好了。

使用while循环从文件读取fscanf 我非常感谢你们回答我的问题。 我发现这个错误是由指针的错误操作引起的。我已经修好了。,c,string,file,printf,scanf,C,String,File,Printf,Scanf,要读取的文件: 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 每行以一个空格开头,以“\n”结尾 我编写了这些代码来读取文件中的行并调用helper(cacheSimulator)func: 函数cacheSimulator是打开文件的起点 cacheSimulator如下 resStruct cacheSimulat

要读取的文件:

 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
每行以一个空格开头,以“\n”结尾

我编写了这些代码来读取文件中的行并调用helper(cacheSimulator)func: 函数
cacheSimulator
是打开文件的起点

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;
}
函数
LoadCache
访问2D数组并处理输出参数
resChar
LoadCache

void LoadCache(int标记,
整数集,
整数块,
内线,
单位*pCache,
resStruct*pRes,
未签名的长*时间,
char-ret[]){
int-get=0;
单位*pTarget=NULL;
而(!get){
对于(int l=0;lvalid&&pTarget->tag==tag){
(压力->点击次数)+;
pTarget->timer=*时间;
get=1;
strcpy(ret,“hit”);
打破
}否则,如果(!pTarget->valid){
(压力->未命中)+;
pTarget->timer=*时间;
pTarget->valid=1;
p目标->标记=标记;
get=1;
strcpy(ret,“miss”);
打破
}
}
如果(!get){
写缓存(标签、集合、块、行、pCache、压力、时间);
(压力->未命中)+;
strcpy(ret,“驱逐小姐”);
get=1;
}
}
++*时间;
}
但我得到了这个输出:

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
与我打开的文件相比,它只读取了前9行。 我有gdb这个可原谅的目标。我发现,当执行while循环读取第10行时,fscanf将返回1,但返回预期的3

gdb输出:

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
如您所见,阅读前9行没有问题。 但是当读取第10行时,
fscanf()
的返回值为1。



尝试执行代码,但出现分段错误。将“open($filepath,r”);”替换为“fopen($filepath,r”);”,并在成功执行此代码后,没有遇到任何错误

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

    }

如果您显示实际的代码而不是注释,这会有所帮助。它只是一个
printf
?有副作用吗?您当然应该检查
scanf
是否返回3,但您所拥有的看起来应该可以工作。什么是
$filepath
?确认!我忘了有些编译器允许在符号名中使用$。你可以做一些事情并不意味着你应该。@williampersell Name Name,因为这是彻头彻尾的恶意行为。你应该并显示真实的代码,
open
无论如何都是错误的。你需要展示一个。您不应该选中
!=EOF
但是
!=3
。2.还要检查
fopen
3的返回值。
op=0的意义是什么;addr=0;字节=0患者回复的thx。我发现调用
LoadCache
时可能有问题。
    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