C 語言實例 - 從檔中讀取一行
從檔中讀取一行。
檔 zaixian.txt 內容:
$ cat zaixian.txt xuhuhu.com google.com
實例
#include <stdio.h>
#include <stdlib.h> // exit() 函數
int main()
{
char c[1000];
FILE *fptr;
if ((fptr = fopen("zaixian.txt", "r")) == NULL)
{
printf("Error! opening file");
// 檔指針返回 NULL 則退出
exit(1);
}
// 讀取文本,直到碰到新的一行開始
fscanf(fptr,"%[^\n]", c);
printf("讀取內容:\n%s", c);
fclose(fptr);
return 0;
}
輸出結果為:
讀取內容: xuhuhu.com