بار پیش فایل را خودم میساختم لاکن یکبار که این کار را با خود c++ انجام دادم پیغام Segmentation fault نگرفتم . چرا نمیدانم !!!
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class record
{
public:
string nam;
int shomare;
};
int main()
{
fopen ("test5.txt","w+");//sakht yek file ba dastrasi read+write
fstream fw("test5.txt");
record temp;
//***************************************************************
temp.nam="mohammad";
temp.shomare=123;
fw.write( (char*) & temp,sizeof(class record) );
temp.nam="mohammad";
temp.shomare=125;
fw.write( (char*) & temp,sizeof(class record) );
temp.nam="mohammad";
temp.shomare=127;
fw.write( (char*) & temp,sizeof(class record) );
temp.nam="mohammad";
temp.shomare=129;
fw.write( (char*) & temp,sizeof(class record) );
temp.nam="mohammad";
temp.shomare=131;
fw.write( (char*) & temp,sizeof(class record) );
//*****************************************************************
fw.seekp(0);
fw.seekp(sizeof(class record),ios::cur);
fw.write( (char*) & temp,sizeof(class record) );
fw.close();
//**************************************************************
fstream fr("test5.txt");
while( fr.read( (char*)& temp,sizeof(class record) ) )
cout<<temp.nam<<" "<<temp.shomare<<endl;
fr.close();
//************************************************************
return 0;
}
------------------------------
چگونگی ساخت یک فایل در c++ :
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/