PDA

View Full Version : سوال: ذخیره اطلاعات در فایل و خواندن از فایل (کمک فوری)



ahmadii
جمعه 08 دی 1391, 13:39 عصر
با عرض سلام.من یه برنامه نوشتم که اطلاعات اساتید رو دریافت می کنه و یه سری عملیات روشون انجام میده حالا می خوام وقتی برنامه بسته میشه اطلاعات در یه فایل ذخیره بشه و وقتی برنامه باز میشه اطلاعات دوباره به برنامه برگرده.لطفا اگر کسی بلده سریع کمکم کنه.

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
////////////////////////////////////////////////////////
class asatid{
public:
long code;
char nam[31];
char family[31];
char date[11];
char reshteh[31];
float nerkh;
float hoghogh;
}st[100],stp;
///////////////////////////////////////////////////////
int i=0;
//////////////////////////////////////////////////////
void input(asatid st[]){
clrscr();
cout<<"please enter code personeli: ";
cin>>st[i].code;
cout<<"please enter nam: ";
gets(st[i].nam);
cout<<"please enter family: ";
gets(st[i].family);
cout<<"please enter tarikh estekhdam: ";
gets(st[i].date);
cout<<"please enter reshteh movred tadris: ";
gets(st[i].reshteh);
cout<<"please enter nerkh saati: ";
cin>>st[i].nerkh;
cout<<"please enter hoghogh mahianeh: ";
cin>>st[i].hoghogh;
i++;
}
/////////////////////////////////////////////////////
void load(asatid st[]){
asatid stp;
ifstream inFile;
inFile.open("student.dat",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";

getch();
return;
}
cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
while(inFile.read((char *) &stp, sizeof(asatid)))
{
stp.show();
cout<<"\n\n====================================\n";
}
inFile.close();

getch();
}
/////////////////////////////////////////////////////
void show(asatid st[]){
clrscr();
for(int j=0;j<i;j++){
cout<<"code personeli: "<<st[j].code<<"\t";
cout<<"nam: "<<st[j].nam<<"\t";
cout<<"family: "<<st[j].family<<"\t";
cout<<"tarikh estekhdam: "<<st[j].date<<"\t";
cout<<"reshteh movrede tadris: "<<st[j].reshteh<<"\t";
cout<<"nerkh saati: "<<st[j].nerkh<<"\t";
cout<<"hoghogh mahianeh: "<<st[j].hoghogh<<endl;
}
getch();
}
/////////////////////////////////////////////////////
void reshtehsp(asatid st[]){
clrscr();
int sw1=0;
char search1[31];
cout<<"please enter reshteh for show :";
gets(search1);
for(int j=0;j<i;j++)
if(strcmp(search1,st[j].reshteh)==0){
cout<<"code personeli: "<<st[j].code<<"\t";
cout<<"nam: "<<st[j].nam<<"\t";
cout<<"family: "<<st[j].family<<"\t";
cout<<"tarikh estekhdam: "<<st[j].date<<"\t";
cout<<"reshteh movrede tadris: "<<st[j].reshteh<<"\t";
cout<<"nerkh saati: "<<st[j].nerkh<<"\t";
cout<<"hoghogh mahianeh: "<<st[j].hoghogh<<endl;
sw1=1;

}
if(sw1==0)
cout<<"ostad not found.";

getch();
}
/////////////////////////////////////////////////////
void sort_family(asatid st[]){
clrscr();
asatid temp;
for(int j=0;j<i;j++)
for(int k=0;k<i-j-1;k++)
if(strcmp(st[k].family,st[k+1].family)>0){
temp=st[k];
st[k]=st[k+1];
st[k+1]=temp;
}
cout<<"asatid are sorted with family.";
getch();
}
/////////////////////////////////////////////////////
void sort_hoghogh(asatid st[]){
clrscr();
asatid temp;
for(int j=0;j<i;j++)
for(int k=0;k<i-j-1;k++)
if(st[k].hoghogh<st[k+1].hoghogh){
temp=st[k];
st[k]=st[k+1];
st[k+1]=temp;
}
cout<<"asatid are sorted with hoghogh.";
getch();
}
/////////////////////////////////////////////////////
void sort_code(asatid st[]){
clrscr();
asatid temp;
for(int j=0;j<i;j++)
for(int k=0;k<i-j-1;k++)
if(st[k].code<st[k+1].code){
temp=st[k];
st[k]=st[k+1];
st[k+1]=temp;
}
cout<<"asatid are sorted with code personeli.";
getch();
}
/////////////////////////////////////////////////////
void search_family(asatid st[]){
clrscr();
int sw=0;
char search[31];
cout<<"please enter family for search :";
gets(search);
for(int j=0;j<i;j++)
if(strcmp(search,st[j].family)==0){
cout<<"code personeli: "<<st[j].code<<"\t";
cout<<"nam: "<<st[j].nam<<"\t";
cout<<"family: "<<st[j].family<<"\t";
cout<<"tarikh estekhdam: "<<st[j].date<<"\t";
cout<<"reshteh movrede tadris: "<<st[j].reshteh<<"\t";
cout<<"nerkh saati: "<<st[j].nerkh<<"\t";
cout<<"hoghogh mahianeh: "<<st[j].hoghogh<<endl;
sw=1;

}
if(sw==0)
cout<<"ostad not found.";

getch();
}
/////////////////////////////////////////////////////
void search_code(asatid st[]){
clrscr();
int sw=0;
int search;
cout<<"please enter code personeli for search :";
cin>>search;
for(int j=0;j<i;j++)
if(search==st[j].code){
cout<<"code personeli: "<<st[j].code<<"\t";
cout<<"nam: "<<st[j].nam<<"\t";
cout<<"family: "<<st[j].family<<"\t";
cout<<"tarikh estekhdam: "<<st[j].date<<"\t";
cout<<"reshteh movrede tadris: "<<st[j].reshteh<<"\t";
cout<<"nerkh saati: "<<st[j].nerkh<<"\t";
cout<<"hoghogh mahianeh: "<<st[j].hoghogh<<endl;
sw=1;
}
if(sw==0)
cout<<"ostad not found.";
getch();
}
/////////////////////////////////////////////////////
void edit(asatid st[]){
clrscr();
int sw=0;
int id;
cout<<"please enter code personeli for edit :";
cin>>id;
for(int j=0;j<i;j++)
if(id==st[j].code){
clrscr();
cout<<"code personeli: "<<st[j].code<<"\t";
cout<<"nam: "<<st[j].nam<<"\t";
cout<<"family: "<<st[j].family<<"\t";
cout<<"tarikh estekhdam: "<<st[j].date<<"\t";
cout<<"reshteh: "<<st[j].reshteh<<"\t";
cout<<"nerkh saati: "<<st[j].nerkh<<"\t";
cout<<"hoghogh mahaneh: "<<st[j].hoghogh<<endl<<endl<<endl<<endl;
cout<<"please enter code personeli: ";
cin>>st[j].code;
cout<<"please enter nam: ";
gets(st[j].nam);
cout<<"please enter family: ";
gets(st[j].family);
cout<<"please enter tarikh estekhdam: ";
gets(st[j].date);
cout<<"please enter reshteh movrede tadris: ";
gets(st[j].reshteh);
cout<<"please enter nerkh saati: ";
cin>>st[j].nerkh;
cout<<"please enter hoghogh mahaneh: ";
cin>>st[j].hoghogh;
sw=1;
}
if(sw==0)
cout<<"ostad not found.";
else
cout<<"ostad edited.";
getch();
}
/////////////////////////////////////////////////////
void del(asatid st[]){
clrscr();
int sw=0;
int id;
cout<<"please enter code personeli for delete :";
cin>>id;
for(int j=0;j<i;j++)
if(id==st[j].code){
for(int k=j;k<i;k++)
st[k]=st[k+1];
i--;
sw=1;
}
if(sw==0)
cout<<"ostad not found.";
else
cout<<"ostad deleted.";
getch();
}
/////////////////////////////////////////////////////
void prhoghogh(asatid st[]){
clrscr();
int saat;
cout<<"please enter saat for hoghogh:";
cin>>saat;
for(int j=0;j<i;j++)
st[j].hoghogh=st[j].nerkh*saat;
cout<<"ok.";
getch();
}
/////////////////////////////////////////////////////
void datesp(asatid st[]){
clrscr();
int sw=0;
char search[5];
cout<<"please enter tarikh estekhdam for search :";
gets(search);
for(int j=0;j<i;j++)
if(st[j].date[0]==search[0]&&st[j].date[1]==search[1]&&st[j].date[2]==search[2]&&st[j].date[3]==search[3]){
cout<<"code personeli: "<<st[j].code<<"\t";
cout<<"nam: "<<st[j].nam<<"\t";
cout<<"family: "<<st[j].family<<"\t";
cout<<"tarikh estekhdam: "<<st[j].date<<"\t";
cout<<"reshteh movrede tadris: "<<st[j].reshteh<<"\t";
cout<<"nerkh saati: "<<st[j].nerkh<<"\t";
cout<<"hoghogh mahianeh: "<<st[j].hoghogh<<endl;
sw=1;

}
if(sw==0)
cout<<"ostad not found.";

getch();
}
/////////////////////////////////////////////////////
void save(asatid st[]){
asatid stp;
ofstream outFile;
outFile.open("student.dat",ios::binary|ios::app);
show(stp);
outFile.write((char *) &st, sizeof(asatid));
outFile.close();
cout<<"\n\nStudent record Has Been Created ";
cin.ignore();
getch();
}
/////////////////////////////////////////////////////
void main(){
load(st);
int ch;
while(1){
clrscr();
cout<<"1)input. "<<endl;
cout<<"2)show all. "<<endl;
cout<<"3)show reshteh asatid. "<<endl;
cout<<"4)sort with family. "<<endl;
cout<<"5)sort with hoghogh. "<<endl;
cout<<"6)sort with code personeli. "<<endl;
cout<<"7)search with family. "<<endl;
cout<<"8)search with code personeli. "<<endl;
cout<<"9)edit. "<<endl;
cout<<"10)delete. "<<endl;
cout<<"11)show hoghogh asatid. "<<endl;
cout<<"12)show asatid spcial date estekhdam. "<<endl;
cout<<"13)Exit. "<<endl;
cout<<"please enter your choice between 1 to 13 : ";
cin>>ch;
switch(ch){
case 1:
input(st);
break;
case 2:
show(st);
break;
case 3:
reshtehsp(st);
break;
case 4:
sort_family(st);
break;
case 5:
sort_hoghogh(st);
break;
case 6:
sort_code(st);
break;
case 7:
search_family(st);
break;
case 8:
search_code(st);
break;
case 9:
edit(st);
break;
case 10:
del(st);
break;
case 11:
prhoghogh(st);
break;
case 12:
datesp(st);
break;
case 13:
save(st);
exit(0);
break;
default:
clrscr();
cout<<"ERROR...........please enter between 1 to 13.";
getch();
break;
}
}
}

hadi0x7c7
جمعه 08 دی 1391, 13:49 عصر
بهترین کار اینه که ارایه یا هر چیزی که داری رو ه صورت باینری ذخیره کنی تا درگیر مثائل فرمت و نحوهی خوندن و .... نشی !

ahmadii
جمعه 08 دی 1391, 14:07 عصر
بهترین کار اینه که ارایه یا هر چیزی که داری رو ه صورت باینری ذخیره کنی تا درگیر مثائل فرمت و نحوهی خوندن و .... نشی !

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

mahak006
جمعه 08 دی 1391, 14:19 عصر
کلا تو ذخیره و خوندن از فایلش موندم اگه ممکنه برنامم رو ویرایش کنید.

از این دو کلاس موجود تو iostream می تونی کمک بگیری. هر کدوم توابع وخصوص خودشون رو دارن . یه مقدار باید روش کار کنی . اسم کلاساشون ifstraem و ostream هست . فقط به قول دوستمون از روش ذخیره و فراخوانی باینری استفاده کن تا زیاد درگیر نشی که فرمت ها یکسان باشن . بره این کار موقع خوندن و ذخیره کردن از ios::binary می تونی تو آرگومان ورودی توابع open و save استفاده کنی . یادت باشه بعد از open فایل رو close کنی . چون تو نوع دادت قابل دسترسی هست و اگه close نکنی حافظه بیشتری درگیر می شه و همچنین بره خروج از برنامه فایل خود به خود بسته نمیشه و بعدا به مشکلاتی ممکنه بر بخوری.

ahmadii
جمعه 08 دی 1391, 14:49 عصر
از این دو کلاس موجود تو iostream می تونی کمک بگیری. هر کدوم توابع وخصوص خودشون رو دارن . یه مقدار باید روش کار کنی . اسم کلاساشون ifstraem و ostream هست . فقط به قول دوستمون از روش ذخیره و فراخوانی باینری استفاده کن تا زیاد درگیر نشی که فرمت ها یکسان باشن . بره این کار موقع خوندن و ذخیره کردن از ios::binary می تونی تو آرگومان ورودی توابع open و save استفاده کنی . یادت باشه بعد از open فایل رو close کنی . چون تو نوع دادت قابل دسترسی هست و اگه close نکنی حافظه بیشتری درگیر می شه و همچنین بره خروج از برنامه فایل خود به خود بسته نمیشه و بعدا به مشکلاتی ممکنه بر بخوری.

لطفا یه نگاهی به برنامم بندازین.تو قسمت load و save همه کارایی که گفتین کردم.ولی خطا داره.اگه ممکنه خطاش رو رفع کنین.ممنون میشم.

mahak006
جمعه 08 دی 1391, 15:19 عصر
لطفا یه نگاهی به برنامم بندازین.تو قسمت load و save همه کارایی که گفتین کردم.ولی خطا داره.اگه ممکنه خطاش رو رفع کنین.ممنون میشم.


خب ورودی ها رو اشتباه دادی دیگه . بر اساس کل برنامت باید ورودی ها رو اشتباه دادی . مثلا file.read آرگومان هاش *char و int هست . در حالی که نوع داده ی asatid قابلیت تبدیل به char* نداره .
میتونی فیلد به فیلد بره هر استاد رو بخونی . مثلا اگه یه int با چند تا char* هست اول با sizeof(int) مقدار بخونی تبدیل کنی به int و بریزی تو مقدار صفرمین int مربوط به استاد صفرم . از تابع file.eof() بره فهمیدن اینکه رسیدی به آخر فایل خونده شده استفاده کن ( داخل حلقه while )

در ضمن file به تنهایی نمی تونه مقدار bool برگردونه که تو if ازش استفاده کردی ( اینا تو تابع لود بودن . )
من با تابع save کار نکردم . شاید اونم یه همچین مشکلاتی داشته باشه . در کل کار کردن با fstream ها سخته و گیج می کنه آدمو .

وقت زیادی می خواد که بتونی درستش کنی .