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

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

كورس C++ المتقدم – الحلقة السادسة: File Handling & Serialization


 

كورس C++ المتقدم – الحلقة السادسة: File Handling & Serialization

#برمجة

مقدمة

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

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

  • Templates
  • Exception Handling
  • Smart Pointers
  • Lambda Functions
  • Multithreading

واليوم سندخل موضوع مهم جداً في أي تطبيق حقيقي:

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

وستتعلم أيضاً مفهوم مهم جداً:

Serialization (تسلسل البيانات)


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

أي برنامج احترافي يحتاج إلى:

✔ حفظ بيانات المستخدم
✔ تخزين إعدادات البرنامج
✔ حفظ نتائج العمليات
✔ إنشاء قواعد بيانات بسيطة

بدون الملفات، كل شيء يضيع عند إغلاق البرنامج.


المكتبة الأساسية

#include <fstream>

أنواع الملفات في C++

النوع الاستخدام
ofstream كتابة داخل ملف
ifstream قراءة من ملف
fstream قراءة + كتابة

أول مثال: كتابة في ملف

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

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

    file << "Hello C++ File Handling" << endl;
    file << "This is line 2";

    file.close();

    return 0;
}

ماذا حدث؟

✔ تم إنشاء ملف اسمه data.txt
✔ تم كتابة النص بداخله
✔ تم إغلاق الملف


قراءة من ملف

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

int main()
{
    string line;

    ifstream file("data.txt");

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

    file.close();

    return 0;
}

لماذا getline؟

لأنها تقرأ الملف سطراً بسطر.


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

ofstream file("data.txt", ios::app);
file << "\nNew Line Added";
file.close();

أوضاع الملفات (Modes)

الوضع المعنى
ios::in قراءة
ios::out كتابة
ios::app إضافة
ios::trunc حذف القديم

مثال عملي: تسجيل أسماء

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

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

    file << "Ahmed\n";
    file << "Ali\n";
    file << "Khaled\n";

    file.close();

    return 0;
}

قراءة الأسماء

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

int main()
{
    string name;
    ifstream file("names.txt");

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

    file.close();

    return 0;
}

التعامل مع الأرقام

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

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

    for(int i = 1; i <= 5; i++)
    {
        file << i << endl;
    }

    file.close();

    return 0;
}

قراءة الأرقام

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

int main()
{
    int num;
    ifstream file("numbers.txt");

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

    file.close();

    return 0;
}

التحقق من فتح الملف

ifstream file("data.txt");

if(!file)
{
    cout << "File not found";
}

ما هو Serialization؟

هو:

تحويل الكائن (Object) إلى بيانات يمكن تخزينها في ملف


مثال بدون Serialization

struct Student
{
    string name;
    int age;
};

المشكلة

لا يمكن حفظ struct مباشرة في ملف نصي بسهولة.


الحل: Serialization


مثال عملي كامل

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

class Student
{
public:
    string name;
    int age;

    void save(ofstream &file)
    {
        file << name << endl;
        file << age << endl;
    }

    void load(ifstream &file)
    {
        getline(file, name);
        file >> age;
        file.ignore();
    }
};

حفظ البيانات

int main()
{
    Student s1;

    s1.name = "Ahmed";
    s1.age = 20;

    ofstream file("student.txt");

    s1.save(file);

    file.close();

    return 0;
}

قراءة البيانات

int main()
{
    Student s1;

    ifstream file("student.txt");

    s1.load(file);

    cout << s1.name << endl;
    cout << s1.age << endl;

    file.close();

    return 0;
}

مثال احترافي: قائمة طلاب

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

class Student
{
public:
    string name;
    int age;

    void save(ofstream &file)
    {
        file << name << endl;
        file << age << endl;
    }
};

int main()
{
    Student s[2];

    s[0].name = "Ahmed";
    s[0].age = 20;

    s[1].name = "Ali";
    s[1].age = 22;

    ofstream file("students.txt");

    for(int i = 0; i < 2; i++)
    {
        s[i].save(file);
    }

    file.close();

    return 0;
}

قراءة قائمة الطلاب

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

class Student
{
public:
    string name;
    int age;

    void load(ifstream &file)
    {
        getline(file, name);
        file >> age;
        file.ignore();
    }
};

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

    while(!file.eof())
    {
        s.load(file);

        cout << s.name << " - " << s.age << endl;
    }

    file.close();

    return 0;
}

أين يستخدم File Handling؟

✔ حفظ بيانات المستخدم
✔ الألعاب (Save Game)
✔ التطبيقات المكتبية
✔ أنظمة التسجيل
✔ برامج المحاسبة


أخطاء شائعة

❌ نسيان file.close()

❌ استخدام getline بعد >>

❌ عدم التحقق من فتح الملف

❌ استخدام eof بطريقة خاطئة


تمرين 1

اكتب برنامج يحفظ 5 أسماء في ملف.


تمرين 2

اقرأ الملف واطبع المحتوى.


تمرين 3

أنشئ نظام حفظ طلاب باستخدام File Handling.


تمرين 4

اصنع برنامج يضيف بيانات بدون حذف القديمة.


تمرين 5

اصنع نظام تسجيل دخول بسيط باستخدام ملف نصي.


ملخص الحلقة السادسة

تعلمنا:

  • File Handling
  • ofstream / ifstream
  • الكتابة والقراءة
  • append mode
  • التحقق من الملفات
  • Serialization
  • حفظ الكائنات في الملفات
  • بناء نظام تخزين بسيط

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

🔥 STL Advanced (المكتبة القياسية المتقدمة)

وسنتعلم:

  • map
  • set
  • unordered_map
  • algorithms
  • sort advanced
  • binary search
  • واستخدام STL مثل المحترفين 🚀

تعليقات

💬 🙋🏻‍♂️