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

C 语言教程 · 2019-02-21 11:11:24

将字符串写入文件。

实例

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

输出结果为:

输入字符串:
codercto.com

打开文件 codercto.txt:

$ cat codercto.txt 
codercto.com

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

必然

必然

[美] 凯文·凯利 / 周峰、董理、金阳 / 译言·东西文库/电子工业出版社 / 2016-1 / 58.00元

凯文·凯利对于经济和社会发展的趋势有着深刻的见解。20年前,他的《失控》一书,便已预见了当下几乎所有的互联网经济热点概念,如:物联网、云计算、虚拟现实、网络社区、大众智慧、迭代等。此次,凯文·凯利在新书《必然》中,提到了未来20年的12种必然趋势。一起来看看 《必然》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具