PDA

View Full Version : مبتدی: خواندن از فایل و ذخیره در struct



meysam_online2012
یک شنبه 11 خرداد 1393, 16:13 عصر
سلام به همگی

من با این کد یه سری داده رو از ورودی میگیرم و توی یه فایل ذخیره می کنم


cout<<"information s"<<i<<"\n";
cout<<"enter id:";
cin>>st[i].id;
cout<<"\nenter name:";
cin.ignore();
cin.getline(st[i].name,20);
cout<<"\nenter family:";
cin.getline(st[i].family,20);
cout<<"\nenter fizik:";
cin>>st[i].fizik;
cout<<"\nenter ryazi:";
cin>>st[i].riazi;
cout<<"\nenter shimi:";
cin>>st[i].shimi;
st[i].avg=(st[i].riazi+st[i].fizik+st[i].shimi)/3;

output_file.open("test.txt",ios::ate);
if(!output_file)
{
cerr<<"File can not open."<<endl;
}
else
{
output_file << st[i].id<< " " << st[i].name <<" " <<st[i].family<<" "<<st[i].riazi<<" "<<st[i].fizik<<" "<<st[i].shimi<<" "<<st[i].avg<< endl;
cout<<"succesful";
}
output_file.close();
i++;


که به این ترتیب توی فایل ذخیره میشه

12 12 12 12hasan ali

حالا چطوری باید اینو از فایل بخونم و توی struct ذخیره کنم؟

Salar Moghaddam
چهارشنبه 14 خرداد 1393, 13:40 عصر
ifstream input_file;
input_file.open("test.txt");
if(!input_file)
{
cerr<<"File can not open."<<endl;
}
else
{
input_file >> st[i].id >> st[i].name >> st[i].family >> st[i].riazi >> st[i].fizik >> st[i].shimi >> st[i].avg;
cout<<"succesful" << endl;
}
input_file.close();
i++;

meysam_online2012
پنج شنبه 15 خرداد 1393, 14:11 عصر
ifstream input_file;
input_file.open("test.txt");
if(!input_file)
{
cerr<<"File can not open."<<endl;
}
else
{
input_file >> st[i].id >> st[i].name >> st[i].family >> st[i].riazi >> st[i].fizik >> st[i].shimi >> st[i].avg;
cout<<"succesful" << endl;
}
input_file.close();
i++;


ممنونم دوست عزیز اگه بخوام شرط بذارم که از ابتدا تا انتها همه رو بخونه و داخل استراکت بذاره باید از چه شرطی اشتفاده کنم؟

بازم تشکر