我的微信
运营不易,
感谢支持!
将字符串写入文件。
#include <stdio.h> #include <stdlib.h> /* exit() 函数 */ int main() { char sentence[1000]; FILE *fptr; fptr = fopen("nowcoder.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; }
输出结果为:
输入字符串: gonshiku.com
打开文件 nowcoder.txt:
$ cat nowcoder.txt gongshiku.com