C言語の例 - ファイルから1行読み取る

C 语言实例C言語の例

ファイルから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 语言实例C言語の例

その他の拡張