[問題] strtok

看板C_and_CPP (C/C++)作者 (gokr)時間16年前 (2009/02/27 23:36), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串1/4 (看更多)
想請問一下大家 不知道為什麼當以下程式執行完 會多印出一行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

02/27 23:44, , 1F
要 fscanf 抓不到值才會 set feof, 所以把 while 迴圈條件
02/27 23:44, 1F

02/27 23:44, , 2F
all true, 中斷條件 feof 改在 fscanf 之後判斷
02/27 23:44, 2F

02/27 23:45, , 3F
又或者直接用 fscanf 的傳回值, 得知有沒有讀到作為判斷
02/27 23:45, 3F
文章代碼(AID): #19g0YA0B (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19g0YA0B (C_and_CPP)