C言語の例 - 文字列をファイルに書き込む
文字列をファイルに書き込む。
例
#include <stdio.h>
#include <stdlib.h>/* exit() 関数 */
int main()
{
char sentence[1000];
FILE *fptr;
fptr = fopen("サンプル.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("入力文字列:\n");
fgets(sentence, (sizeof sentence / sizeof sentence[0]), stdin);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
出力結果は:
输入字符串: example.com
ファイル サンプル.txt を開く:
$ cat サンプル.txt example.comその他の拡張
C言語の例