سلام
ممکنه کمکم کنید؟
من یه برنامه با فایل و struct نوشتم که منوی اون 6 تا گزینه داره
ولی متاسفانه 2 و3 که delete و edit هست درست کار نمیکنه
مشکل اینجاست که برای delete مینویسه حذف شد ولی وقتیshow all میکنم دوباره نمایش میده و حذف نشده
برای edit هم اطلاعات قبلی رو حذف میکنه ولی اطلاعات جدید رو جایگزین نمیکنه و در show all اونو نمایش نمیده
بعبارتی فقط حذف میکنه و جایگزین نمیکنه
اگه ممکنه کمک کنید
ممنون
#include <fstream>
#include <conio.h>
#include <iostream>
using namespace std;
void add();
void edit();
void sort();
void show();
void delet();
void search();
struct student
{
char fname[20];
char lname[35];
long int number;
float ave;
int f;
};
int main()
{
int y;
while(1)
{
system("cls");
system("color 0a");
cout<<"1-add student"<<endl;
cout<<"2-edit student"<<endl;
cout<<"3-delete student"<<endl;
cout<<"4-search"<<endl;
cout<<"5-sort"<<endl;
cout<<"6-show all"<<endl;
cout<<"7-exit"<<endl;
cout<<endl<<endl<<endl;
cout<<"enter your selection(1-7)";
cin>>y;
switch(y)
{
case 1:
add();
break;
case 2:
edit();
break;
case 3:
delet();
break;
case 4:
search();
break;
case 5:
sort();
break;
case 6:
show();
break;
case 7:
exit(0);
default:
system("cls");
cout<<"Enter a valid number.";
_getch();
}
}
return 0;
}
//-------------------------------------------------------------------------------------
void add()
{
system("cls");
int n;
student z;
ofstream file("a.dat",ios::binary|ios::app);
if(!file)
{
cout<<"can not open the file \n";
getch();
return;
}
cout<<"enter number of students : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter fname and lname and number and ave : \n";
cin>>z.fname>>z.lname>>z.number>>z.ave;
z.f=1;
file.write((char *)&z,sizeof(student));
}
file.close();
getch();
}
//...............................''''''''''''''''''' ''''''''''''''''''''''''
void edit()
{
system("cls");
student z,r,*pst;
int c,i;
long int number1;
cout<<"enter number1 :\n";
cin>>number1;
ifstream file("a.dat",ios::in|ios::binary);
if(!file)
{
cout<<"can not open the file \n";
getch();
exit(0);
}
file.read((char *)&z,sizeof(student));
for(c=0;!file.eof();C++)
{
file.read((char *)&z,sizeof(student));
}
file.close();
pst=new student[c];
ifstream infile("a.dat",ios::in|ios::binary);
if(!infile)
{
cout<<"can not open file";
getch();
exit(0);
}
infile.read((char*)pst,c*sizeof(student));
infile.close();
ofstream outfile("b.dat",ios::out|ios::binary);
if(!outfile)
{
cout<<"can not open file";
getch();
return;
}
for(i=0;i<c;i++){
if(pst[i].number==number1&& pst[i].f==1)
{
cout<<pst[i].fname<<"\n"<<pst[i].lname<<"\n"<<pst[i].number<<"\n"<<pst[i].ave<<"\n\n";
cout<<"enter new fname and lname and number and ave :\n";
cin>>r.fname>>r.lname>>r.number>>r.ave;
outfile.write((char *)&r,sizeof(student));
}
else{
r=pst[i];
outfile.write((char *)&r,sizeof(student));
}}
outfile.close();
remove("a.dat");
rename("b.dat","a.dat");
getch();
}
//------------------------------------------------------------------------
void delet()
{
long int number1;
system("cls");
fstream infile("a.dat",ios::in|ios::out|ios::binary);
if(! infile)
{
cout<<"can not open the file";
getch();
exit(0);
}
student z;
cout<<"please Enter ID for delete";
cin>>number1;
infile.read((char*)&z,sizeof(student));
while(!infile.eof())
{
if(z.number=number1){
z.f=0;
infile.seekp(-sizeof(student),ios::cur);
infile.write((char *)&z,sizeof(student));
cout<<"\t\t\t\n\deleted";
break;
}
infile.read((char *)&z,sizeof(student));}
getch();
}//en of deleted
//************************************************** ****************************
//************************************************** ****************************
void search()
{
system("cls");
ifstream file("a.dat",ios::in|ios::binary);
if(!file)
{
cout<<"can not open the file \n";
getch();
return;
}
student z;
long int number1;
cout<<"enter number1 :\n";
cin>>number1;
file.read((char *)&z,sizeof(student));
while(!file.eof())
{
if(z.number==number1 && z.f==1)
cout<<z.fname<<"\n"<<z.lname<<"\n"<<z.number<<"\n" <<z.ave<<"\n\n";
file.read((char *)&z,sizeof(student));
}
file.close();
getch();
}
void sort()
{
system("cls");
int c;
student st,*pst;
ifstream file("a.dat",ios::in|ios::binary);
if(!file)
{
cout<<"can not open file";
getch();
return;
}
file.read((char*)&st,sizeof(student));
for(c=0;!file.eof();C++)
{
file.read((char*)&st,sizeof(student));
}
file.close();
pst = new student[c];
ifstream infile("a.dat",ios::in|ios::binary);
if(!infile)
{
cout<<"can not open file";
getch();
return;
}
infile.read((char*)pst,c*sizeof(student));
infile.close();
for(int j=c-1;j>0;j--)
{
for(int i=0;i<j;i++)
{
if(pst[i].ave<pst[i+1].ave)
{
st = pst[i];
pst[i] = pst[i+1];
pst[i+1] = st;
}
}
}
ofstream outfile("b.dat",ios::out|ios::binary);
if(!outfile)
{
cout<<"can not open file";
getch();
return;
}
outfile.write((char*)pst,c*sizeof(student));
outfile.close();
remove("a.dat");
rename("b.dat","a.dat");
getch();
}
void show()
{
system("cls");
student z;
ifstream file("a.dat",ios::binary|ios::in);
if(!file)
{
cout<<"can not open the file \n";
getch();
return;
}
file.read((char *)&z,sizeof(student));
while(!file.eof())
{
if(z.f==1)
{
cout<<z.fname<<"\n"<<z.lname<<"\n"<<z.number<<"\n" <<z.ave<<"\n\n";
}
file.read((char *)&z,sizeof(student));
}
file.close();
getch();
}