我試圖完全讀取文件的第一行,直到\n
並將其存儲在一個變量中。然後我嘗試讀取文件的第二行,但沒有給出正確的輸出。我不確定發生了什麼事。它讀取空白,或者可能是在fscanf()之後移動的文件指針?當我用fscanf讀第二行時,爲什麼會得到空字符?
的abc.txt文件包含:
>hello test file<br>
1
但輸出(我在printf中得到)是:
status:
>pwd :hello test file
那麼,爲什麼狀態在這裏失蹤?
這裏是我的程序:
#include <stdio.h>
#include <string.h>
int main()
{
char status,pwd[30];
FILE *fp;
fp=fopen("abc.txt","r");
if(fp == NULL)
{
printf("Cannot open file ");
fclose(fp);
return 0;
}
fscanf(fp,"%29[^\n]",pwd);
fscanf(fp,"%c",&status);
fclose(fp);
printf("\n Status : %c pwd: %s",status,pwd);
}
put'fgetc(fp);'介於最後兩個fscanf()之間,希望能夠解決您的問題 – cakil 2013-04-08 18:35:57