[問題] 開檔讀資料直到換行停止
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
請問如何開檔讀取資料直到換行字元出現停止讀取並繼續執行程式
餵入的資料(Input):
10 20 30 40 50
100
預期的正確結果(Expected Output):
10
20
30
40
50
錯誤結果(Wrong Output):
10
20
30
40
50
100
程式碼(Code):(請善用置底文網頁, 記得排版)
while( fscanf( file, "%d", &temp ) != EOF ) printf( "%d", temp );
補充說明(Supplement):
除了用 fscanf 之外
還用了 fgets + sscanf 搭配
似乎也不可行
fgets + strtok + atoi
永遠只印出10來
不會自動往前走
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.135.24.88
找到方法了
程式碼如下
char tempStr[10000];
char *delim = " \t\n";
char *tempTok = NULL;
int tempNum;
fgets( tempStr, 10000, file );
tempTok = strtok( tempStr, delim );
while( tempTok != NULL ) {
tempNum = atoi( tempTok );
printf( "%d\n", tempNum );
tempTok = strtok( NULL, delim );
}
只是不解為什麼到數第二行一定要 strtok( NULL, delim );
而不是 strtok( tempStr, delim );
※ 編輯: iWRZ 來自: 140.135.24.88 (04/11 23:05)
推
04/11 23:09, , 1F
04/11 23:09, 1F
→
04/11 23:09, , 2F
04/11 23:09, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章