Text files: write then read · Text files: เขียนแล้วอ่าน
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 เก็บข้อมูลบน disk, ดังนั้นมันยังคงอยู่หลังจากโปรแกรมของคุณหยุดทำงาน
- คุณทำงานกับ file ผ่าน
FILE *— pointer ที่คุณได้รับจากfopen - ในงานเหล่านี้คุณ เขียน file, แล้ว อ่าน กลับมาในโปรแกรมเดียวกัน, ดังนั้นทุกอย่างจึง self-contained
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, อ่านหรือเขียน, แล้ว ปิด ด้วย 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.
เปิดและปิด
fopen("data.txt", "w")เปิด file สำหรับ การเขียน (มันสร้างหรือล้างไฟล์)fopen("data.txt", "r")เปิดมันสำหรับการ อ่าน- เสมอ
fclose(f)เมื่อคุณทำเสร็จ, เพื่อให้ข้อมูลถูกบันทึกและ file ถูกปล่อย
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, แต่ argแรกคือFILE *fprintf(f, "%d\n", x);เขียนเลขxและ newline เข้า filef- เขียนแต่ละค่าบรรทัดเดียวเพื่อให้อ่านกลับได้ง่าย
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ที่ปลาย- Loop จนกว่าการอ่านจะล้มเหลวเพื่อประมวลผลทั้ง 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.
ข้อผิดพลาดที่พบบ่อย
- ตรวจสอบว่า
fopenไม่ได้คืนค่าNULLก่อนอ่านหรือเขียน - เสมอ
fclosefile เมื่อคุณทำเสร็จ
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.
ลองดูเลย
- งานเหล่านี้เป็น full programs: เขียนภายใน
main, และ Check answer ทดสอบ output - เขียน file ก่อน,
fclose, แล้วเปิดอีกครั้งสำหรับการอ่านและ loop จนกว่าการอ่านจะล้มเหลว
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่