Text files: write then read · File teks: tulis lalu baca
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 menyimpan data setelah program berakhir
- File menyimpan data di disk, sehingga tetap ada setelah program Anda berhenti.
- Anda berinteraksi dengan file melalui
FILE *— sebuah pointer yang Anda dapatkan darifopen. - Dalam tugas-tugas ini Anda menulis file, lalu membacanya kembali dalam program yang sama, sehingga semuanya mandiri.
Writing then reading a file · Menulis lalu membaca file
A file is used in three steps: open it with fopen, read or write, then close it with fclose. · Sebuah file digunakan dalam tiga langkah: buka dengan fopen, baca atau tulis, kemudian tutup dengan fclose.
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.
Membuka dan menutup
fopen("data.txt", "w")membuka file untuk penulisan (menciptakan atau mengosongkan file).fopen("data.txt", "r")membukanya untuk pembacaan.- Selalu
fclose(f)ketika selesai, agar data tersimpan dan file dibebaskan.
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.
Menulis dengan fprintf
fprintfbekerja sepertiprintf, namun argumen pertamanya adalahFILE *.fprintf(f, "%d\n", x);menulis angkaxdan baris baru ke dalam filef.- Tulis setiap nilai pada baris tersendiri agar mudah dibaca kembali.
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.
Membaca dengan fscanf dan fgets
fscanf(f, "%d", &x)membaca satu angka; ia mengembalikan1jika berhasil.fgets(buf, size, f)membaca satu baris teks ke dalambuf; ia mengembalikanNULLdi akhir.- Lakukan perulangan hingga pembacaan gagal untuk memproses seluruh file.
#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.
Kesalahan umum
- Pastikan bahwa
fopentidak mengembalikanNULLsebelum membaca atau menulis. - Selalu
fclosefile ketika selesai.
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.
Sekarang Anda coba
- Tugas-tugas ini adalah program lengkap: tulis di dalam
main, dan Periksa jawaban menguji outputnya. - Tulis file terlebih dahulu,
fclose, kemudian buka lagi untuk membaca dan lakukan perulangan hingga pembacaan gagal.
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). · Di main: tulis angka 10, 20, 5, 7 (satu per baris) ke dalam file, tutup, lalu buka kembali dan jumlahkan dengan fscanf. Cetak total (42).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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). · Di main: tulis tiga baris teks ke dalam file, tutup, lalu buka kembali dan hitung barisnya dengan fgets. Cetak jumlah (3).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.