Text files: write then read · 文本文件:先写后读
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Files keep data after the program ends
- A file stores data on disk, so it is still there after your program stops.
- You work with a file through a
FILE *— a pointer you get fromfopen. - In these tasks you write a file, then read it back in the same program, so everything is self-contained.
中文
文件让数据在程序结束后保留
- 文件把数据存在磁盘上,所以程序停止之后它还在。
- 你通过一个
FILE *来操作文件 —— 这是fopen给你的一个指针。 - 在这些任务里,你先写一个文件,再在同一个程序里把它读回来,所以一切都是自包含的。
Explore · 探索
Writing then reading a file · 先写文件,再读文件
A file is used in three steps: open · 开放的 it with fopen, read or write, then close · 关闭 it with fclose. · 用文件分三步:用 fopen 打开,读或写,然后用 fclose 关闭。
English
Opening and closing
fopen("data.txt", "w")opens a file for writing (it creates or empties the file).fopen("data.txt", "r")opens it for reading.- Always
fclose(f)when you are done, so the data is saved and the file is released.
中文
打开与关闭
fopen("data.txt", "w")以写入方式打开文件(它会创建文件或清空文件)。fopen("data.txt", "r")以读取方式打开。- 用完后总是
fclose(f),这样数据才会保存,文件才会被释放。
English
Writing with fprintf
fprintfworks likeprintf, but its first argument is theFILE *.fprintf(f, "%d\n", x);writes the numberxand a newline into the filef.- Write each value on its own line so it is easy to read back.
中文
用 fprintf 写入
fprintf和printf很像,只是它的第一个参数是FILE *。fprintf(f, "%d\n", x);把数字x和一个换行写进文件f。- 每个值单独写一行,这样读回来时更容易。
English
Reading with fscanf and fgets
fscanf(f, "%d", &x)reads one number; it returns1when it succeeds.fgets(buf, size, f)reads one line of text intobuf; it returnsNULLat the end.- Loop until the read fails to process the whole file.
中文
用 fscanf 和 fgets 读取
fscanf(f, "%d", &x)读一个数字;成功时返回1。fgets(buf, size, f)读一行文本到buf;到末尾时返回NULL。- 循环直到读取失败,就能处理整个文件。
#include <stdio.h>
int main(void) {
FILE *f = fopen("nums.txt", "w");
fprintf(f, "3\n4\n");
fclose(f);
f = fopen("nums.txt", "r");
int x, total = 0;
while (fscanf(f, "%d", &x) == 1) {
total += x;
}
fclose(f);
printf("%d\n", total); // 7
return 0;
}
English
Common mistakes
- Check that
fopendid not returnNULLbefore reading or writing. - Always
fclosethe file when you are done.
中文
常见错误
- 读写前先检查
fopen没有返回NULL。 - 用完一定要
fclose。
English
Now you try
- These tasks are full programs: write inside
main, and Check answer tests the output. - Write the file first,
fcloseit, then open it again for reading and loop until the read fails.
中文
现在轮到你了
- 这些任务是完整的程序:在
main里面写代码,用 Check answer(检查答案) 来测试输出。 - 先写文件,
fclose它,然后再打开来读取,循环直到读取失败。
In main: write the numbers 10, 20, 5, 7 (one per line) to a file, close it, then open it again and add them up with fscanf. Print the total (42). · 在 main 里面:把数字 10、20、5、7(每个一行)写到一个文件里,关闭它,然后再打开,用 fscanf 把它们加起来。打印总和(42)。
Click Run to see the output here. · 点击“运行”查看此处输出。
In main: write three lines of text to a file, close it, then open it again and count the lines with fgets. Print the count (3). · 在 main 里面:把三行文本写到一个文件里,关闭它,然后再打开,用 fgets 数行数。打印行数(3)。
Click Run to see the output here. · 点击“运行”查看此处输出。