作者qeadzc123 (gokr)
看板C_and_CPP
標題[問題] strtok
時間Fri Feb 27 23:36:41 2009
想請問一下大家
不知道為什麼當以下程式執行完
會多印出一行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
1F:推 ledia:要 fscanf 抓不到值才會 set feof, 所以把 while 迴圈條件 02/27 23:44
2F:→ ledia:all true, 中斷條件 feof 改在 fscanf 之後判斷 02/27 23:44
3F:→ ledia:又或者直接用 fscanf 的傳回值, 得知有沒有讀到作為判斷 02/27 23:45