[英]fscanf reading from file with while loop
要讀取的文件:
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: function 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;
}
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;
}
但我得到了這個 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
與我打開的文件相比,它只讀取了前 9 行。 我有 gdb 可執行對象。 我發現當執行 while 循環讀取第 10 行時,fscanf 將返回 1,但 3 是預期的。
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
如您所見,閱讀前 9 行沒有問題。 但是當讀取第 10 行時, fscanf()
的返回值為 1。
嘗試執行代碼,出現分段錯誤。 替換“打開($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;
}
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.