Text files: write then read · テキストファイル:書き込み然后读取
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から得られるポインタです。 - これらのタスクでは、ファイルに書き込み、同じプログラム内で読み戻すため、すべてを自身で完結させます。
Writing then reading a file · ファイルを書き込んだ后读取
A file is used in three steps: open it with fopen, read or write, then close it with fclose. · ファイルは3つのステップで使用される:fopenでopenし、readまたはwriteした後、fcloseでcloseする。
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)を呼び出してデータを保存し、ファイルを解放してください。
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に書き込みます。- 各値を別々の行に書き込むと、後から読み戻しやすいです。
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つの数値を読み込み、成功すると1を返します。fgets(buf, size, f)はテキストの1行を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;
}
Common mistakes
- Check that
fopendid not returnNULLbefore reading or writing. - Always
fclosethe file when you are done.
よくあるミス
- 読み込みや書き込みの前に、
fopenがNULLを返していないか確認してください。 - 作業が終わったら必ず
fcloseを呼び出してファイルを閉じてください。
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の内部に記述し、回答を確認で出力をテストします。 - まずファイルを書き込み、
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(各行1つ)をファイルに書き込み、closeしてから再びopenしてfscanfで合計する。合計(42)をprintする。
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で、3行のテキストをファイルに書き込み、closeしてから再びopenしてfgetsで行数をcountする。count(3)をprintする。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。