将字符串写入文件。
实例
include
include / exit() 函数 /
int main()
{
char sentence[1000];
FILE *fptr;
fptr = fopen("runoob.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;
}
输出结果为:
输入字符串:
runoob.com
打开文件 runoob.txt:
$ cat runoob.txt
runoob.com