...
درسنا اليوم كيف نطلع المتغيرات من البرنامج ------> الى ملف
ثم قراءة المتغيرات من الملف ----------> الى البرنامج
أول شي: سنحتاج الى مكتبة الـ fstream في السي++
كود PHP:
#include <fstream> // for File I/O
ثم:
1. الكتابة الى ملف:
للكتابة الى ملف سنحتاج الى تعريفه اولا:
كود PHP:
ofstream file; // file out-put: from local variable to file
file.open("file.dat");
البرنامج سيقوم بإنشاء ملف اسمه file ونوعه dat
بعدها سأمل الملف file ببيانات .. قد تكون اي شي int | char | string ..
مثلا انا اريد ان املأ الملف بارقام 1 و 0 ..
كود PHP:
for(int i = 0; i <= 10000; i++)
{
file<<i%2 <<" ";
}
الان اغلق الملف. من البرنامج كي يتيح لبرامج اخرى استعماله
2. القراءة من ملف:
التعريف:
كود PHP:
ifstream rfile; // file in-put: from file to a local variable
rfile.open("file.dat")
المتغير الذي سنقرآه كل مره:
القراءه الى اخر الملف: باستخادم ال eof
كود PHP:
while(!rfile.eof()) //while we did not reach the end of file -- read the file
{
rfile>>c; cout<<c<<" ";
}
اغلاق الملف
3. مثال متكامل:
كود PHP:
/*
ßá ÇáÍÞæÞ ãÍÝæÙÉ ÇÒÈß ÇáãäÊÏì
AZPC | Montada.com | AddaxSoft.com
*/
#include <iostream> // for cout and cin
#include <fstream> // for File I/O
using namespace std;
int main(int argc, char *argv[])
{
//###########################################################
ofstream file; // file out-put: from local variable to file
file.open("file.dat");
for(int i = 0; i <= 10000; i++)
{
file<<i%2 <<" ";
}
file.close();
system("pause");
ifstream rfile; // file in-put: from file to a local variable
rfile.open("file.dat");
char c;
while(!rfile.eof()) //while we did not reach the end of file -- read the file
{
rfile>>c; cout<<c<<" ";
}
rfile.close();
//############################################################
cout<<"\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
إنتهى .. دعواتكم
عند نقل مواضيعي أرجو عدم التغيير فيها وذكر مصدرها الكامل وعدم التلاعب باي رابط يحفظ حقوقي الشخصيه. وللزائر الحرية في نقلها اذا طبق هذه الشروط
...