انجمن‌های فارسی اوبونتو

کمک و پشتیبانی => برنامه‌سازی => نویسنده: doomhammer65ir در 19 خرداد 1390، 05:08 ب‌ظ

عنوان: c++ : نوشتن در یک فایل
ارسال شده توسط: doomhammer65ir در 19 خرداد 1390، 05:08 ب‌ظ
روز خوش
میخواهیم در یک فایل رکورد هایی بنویسیم .  پس از چند بار نوشتن رکوردها میخواهیم یکی از رکورد ها را ویرایش کنیم .
بنده این روش را بکار بستم :
نخست اشاره گر نوشتن ( seekp )‌ را به آغاز رکوردی که باید ویراسته شود بردم و سپس چیزی که میخواستم نوشتم . لاکن پیغام   Segmentation fault میگیرم . دوستان مهر ورزیده نگاهی به کد بیندازند :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class record
{
public:
string nam;
int shomare;
};

int main()
{
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++ : نوشتن در یک فایل
ارسال شده توسط: doomhammer65ir در 19 خرداد 1390، 07:05 ب‌ظ
بار پیش فایل را خودم میساختم لاکن یکبار که این کار را با خود 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/