PDA

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



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

#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;
}
}
}

Borland.C
جمعه 08 دی 1391, 21:48 عصر
به نظرم راحتر بود اگه به جای کلاس از struct استفاده می کردی و از توابع file
البته اگه اجباری در استفاده از کلاس نیست

bendavis
یک شنبه 10 دی 1391, 23:40 عصر
کار با فایلها روش های زیادی دارد
ساده ترین دستور زیر است (به کمک فایهای متنی)
به کمک دستور fopen

bendavis
یک شنبه 10 دی 1391, 23:41 عصر
نمی توان در این محیط خوب تایپ کرد مگرنه کد را با اخنصار مینوشتم

mahak006
دوشنبه 11 دی 1391, 09:54 صبح
نمی توان در این محیط خوب تایپ کرد مگرنه کد را با اخنصار مینوشتم


کد رو تو تگ c++ بذار . موقع تایپ کردن این تگ تو نوار ابزار تایپ تو فروم هستش . این طور کد خوانا تر می شه .

kingsezar
دوشنبه 11 خرداد 1394, 04:05 صبح
با عرض سلام.من یه برنامه نوشتم که اطلاعات اساتید رو دریافت می کنه و یه سری عملیات روشون انجام میده حالا می خوام وقتی برنامه بسته میشه اطلاعات در یه فایل ذخیره بشه و وقتی برنامه باز میشه اطلاعات دوباره به برنامه برگرده.لطفا اگر کسی بلده سریع کمکم کنه برنامه رو ویرایش کنه.

#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;
}
}
}