看板 C_and_CPP 關於我們 聯絡資訊
想請問一下大家 不知道為什麼當以下程式執行完 會多印出一行B 有什麼方法可以解決 test.txt檔案為 A(10) A(2) A(33) A(65) B(4) B(2) #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { FILE *fp; char str[10]; char *k; fp=fopen("test.txt","r"); while(!feof(fp)) { fscanf(fp,"%s",str); k=strtok(str,"()"); while(k!=NULL) { printf("%s\n",k); k=strtok(NULL,"()"); } } fclose(fp); system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.123.170
ledia:要 fscanf 抓不到值才會 set feof, 所以把 while 迴圈條件 02/27 23:44
ledia:all true, 中斷條件 feof 改在 fscanf 之後判斷 02/27 23:44
ledia:又或者直接用 fscanf 的傳回值, 得知有沒有讀到作為判斷 02/27 23:45