🔥 أحدث الأخبار

موقع يهتم بكل ماهو جديد في عالم التكنولوجيا والرياضة

الحلقة التاسعة في تعلم C++: التعامل مع الملفات (File Handling)

 


الحلقة التاسعة في تعلم C++: التعامل مع الملفات (File Handling)

#برمجة

مقدمة

مرحباً بك في الحلقة التاسعة من سلسلة تعلم C++ للمبتدئين.

في الحلقات السابقة تعلمنا:

  • المتغيرات
  • الشروط
  • الحلقات
  • الدوال
  • المصفوفات
  • النصوص
  • المؤشرات
  • البرمجة الكائنية (OOP)

واليوم سننتقل لشيء مهم جداً في التطبيقات الحقيقية:

التعامل مع الملفات (File Handling)


لماذا نحتاج الملفات؟

حتى الآن كل البيانات التي نستخدمها في البرامج:

  • تختفي عند إغلاق البرنامج

لكن في البرامج الحقيقية نحتاج:

  • حفظ بيانات المستخدم
  • حفظ كلمات المرور
  • حفظ نتائج الطلاب
  • حفظ إعدادات البرنامج

وهنا نستخدم الملفات.


ما هو الملف في البرمجة؟

الملف هو مكان على الجهاز يتم فيه تخزين البيانات بشكل دائم.


مكتبة الملفات في C++

#include <fstream>

أنواع الملفات

في C++ يوجد 3 أنواع رئيسية:

  • ofstream → كتابة داخل ملف
  • ifstream → قراءة من ملف
  • fstream → كتابة وقراءة

أولاً: كتابة داخل ملف

مثال بسيط

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream file;

    file.open("data.txt");

    file << "Hello File";

    file.close();

    return 0;
}

شرح المثال

  • open → فتح الملف
  • << → كتابة داخل الملف
  • close → إغلاق الملف

أين يتم حفظ الملف؟

في نفس مكان تشغيل البرنامج.


كتابة بيانات أكثر

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream file("data.txt");

    file << "Name: Ahmed" << endl;
    file << "Age: 20" << endl;

    file.close();

    return 0;
}

ثانياً: قراءة من ملف

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file;
    string text;

    file.open("data.txt");

    while(getline(file, text))
    {
        cout << text << endl;
    }

    file.close();

    return 0;
}

شرح الكود

  • getline → قراءة سطر كامل
  • while → قراءة حتى نهاية الملف

مثال قراءة كلمة واحدة

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file;
    string word;

    file.open("data.txt");

    while(file >> word)
    {
        cout << word << endl;
    }

    file.close();

    return 0;
}

ثالثاً: إضافة بيانات بدون حذف القديم

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream file("data.txt", ios::app);

    file << "New Line Added" << endl;

    file.close();

    return 0;
}

ios::app

تعني الإضافة (Append) بدون مسح المحتوى القديم.


مثال عملي: نظام تسجيل مستخدمين

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string name;
    int age;

    cout << "Enter name: ";
    cin >> name;

    cout << "Enter age: ";
    cin >> age;

    ofstream file("users.txt", ios::app);

    file << name << " " << age << endl;

    file.close();

    cout << "Saved Successfully";

    return 0;
}

مثال قراءة المستخدمين

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file;
    string name;
    int age;

    file.open("users.txt");

    while(file >> name >> age)
    {
        cout << "Name: " << name << " Age: " << age << endl;
    }

    file.close();

    return 0;
}

التحقق من وجود ملف

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file("data.txt");

    if(file.is_open())
    {
        cout << "File exists";
    }
    else
    {
        cout << "File not found";
    }

    return 0;
}

حذف محتوى ملف

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream file("data.txt", ios::trunc);

    file.close();

    return 0;
}

الفرق بين الأنواع

النوع الوظيفة
ofstream كتابة
ifstream قراءة
fstream الاثنين

مثال fstream (قراءة وكتابة)

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream file;

    file.open("data.txt", ios::in | ios::out | ios::app);

    file << "Hello Again" << endl;

    file.close();

    return 0;
}

تطبيق عملي مهم جداً

نظام درجات الطلاب

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string name;
    int mark;

    cout << "Enter name: ";
    cin >> name;

    cout << "Enter mark: ";
    cin >> mark;

    ofstream file("students.txt", ios::app);

    file << name << " " << mark << endl;

    file.close();

    cout << "Saved";

    return 0;
}

عرض النتائج

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file("students.txt");

    string name;
    int mark;

    while(file >> name >> mark)
    {
        cout << name << " => " << mark << endl;
    }

    file.close();

    return 0;
}

أخطاء شائعة

❌ نسيان close()
❌ فتح ملف بدون التأكد
❌ استخدام مسار خاطئ
❌ خلط بين قراءة وكتابة


تمارين

  1. برنامج يخزن أسماء 5 أشخاص في ملف
  2. برنامج يقرأ الملف ويعرض البيانات
  3. برنامج يضيف بيانات بدون حذف القديم
  4. برنامج يحسب عدد الأسطر في ملف

ملخص الحلقة التاسعة

في هذه الحلقة تعلمنا:

  • ما هي الملفات
  • كتابة الملفات
  • قراءة الملفات
  • إضافة بيانات
  • حذف محتوى ملف
  • تطبيقات عملية

في الحلقة العاشرة سنتعلم:

🔥 الخوارزميات (Algorithms) + مشاريع قوية في C++
مثل:

  • البحث
  • الفرز
  • مشاريع نظام كامل للمستخدمين




تعليقات

💬 🙋🏻‍♂️