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. · اضغط تشغيل لرؤية المخرجات هنا.