C 语言实例 - 将字符串写入文件

C 语言实例

将字符串写入文件。

实例

#include <stdio.h> #include <stdlib.h> /* exit() 函数 */ int main() { char sentence[1000]; FILE *fptr; fptr = fopen("codexy.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; }

输出结果为:

输入字符串:
codexy.cn

打开文件 codexy.txt:

$ cat codexy.txt 
codexy.cn

C 语言实例