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

查看所有标签

Realm of Racket

Realm of Racket

Matthias Felleisen、Conrad Barski M.D.、David Van Horn、Eight Students Northeastern University of / No Starch Press / 2013-6-25 / USD 39.95

Racket is the noble descendant of Lisp, a programming language renowned for its elegance and power. But while Racket retains the functional goodness of Lisp that makes programming purists drool, it wa......一起来看看 《Realm of Racket》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具