C言語の例 - ファイルから1行読み取る
ファイルから1行読み取ります。
ファイル サンプル.txt の内容:
$ cat サンプル.txt example.com google.com
例
#include <stdio.h>
#include <stdlib.h>// exit() 関数
int main()
{
char c[1000];
FILE *fptr;
if ((fptr = fopen("サンプル.txt", "r")) == NULL)
{
printf("Error! opening file");
// ファイルポインタが NULL を返した場合は終了
exit(1);
}
// 新しい行が始まるまでテキストを読み取る
fscanf(fptr,"%[^\n]", c);
printf("読み取った内容:\n%s", c);
fclose(fptr);
return 0;
}
出力結果は:
读取内容: example.comその他の拡張
C言語の例