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. · קובץ משמש בשלושה שלבים: פתיחה עם 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")פותח קובץ ל-כתיבה (הוא יוצר או מנקה את הקובץ).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כאשר מצליח.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;
}
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 (אחד בכל שורה) לקובץ, סגור אותו, פתח אותו שוב וחבר אותם עם 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. · לחץ על הרץ כדי לראות את התוצא כאן.