نقل قول: مشکل در delete , edit
البته جای delete بالا میخوام از این کد استفاده کنم
ولی اینم مشکل داره
ممکنه برای رفع مشکل edit کد بالا و delete این کد کمکم کنید؟؟؟
#include<fstream>
#include<iostream>
#include<conio.h>
using namespace std;
void add();
void show();
void search();
void delet();
struct student{
char fname[20];
char lname[20];
long int stn;
float ave;
};
int main()
{
system("cls");
int y;
while(1)
{
system("cls") ;
system("color 3b");
cout<<"1-add student "<<endl;
cout<<"2-show student "<<endl;
cout<<"3-search student "<<endl;
cout<<"4-delete student "<<endl;
cout<<"5-exit"<<endl;
cout<<"\n\n";
cout<<"enter your number of selection:";
cin>>y;
switch(y)
{
case 1:
add();
break;
case 2:
show();
break;
case 3:
search();
break;
case 4:
delet();
break;
case 5:
exit(0);
default:
system("cls");
cout<<"enter a value number:";
_getch();
}
}
return 0;
}
//------------------------------------------------
void add()
{
system("cls");
int n;
student st;
ofstream file("a.dat",ios::binary|ios::app);
if(!file)
{
cout<<"can not open the file.\n";
getch();
return;
}
cout<<"enter number of student:";
cin>>n;
for(int i=0;i<n;i++){
cout<<"enter fname lname average st number:";
cin>>st.fname>>st.lname>>st.ave>>st.stn;
file.write((char*)&st,sizeof(student));
}
file.close();
getch();
}
//------------------------------------------------------
void show()
{
system("cls");
student st;
ifstream file("a.dat",ios::binary|ios::in);
if(!file)
{
cout<<"can not open the file.\n";
getch();
return;
}
file.read((char*)&st,sizeof(student));
while(!file.eof())
{
cout<<st.fname<<"\t"<<st.lname<<"\t"<<st.ave<<"\t" <<st.stn<<endl;
file.read((char*)&st,sizeof(student));
}
file.close();
getch();
}
//-------------------------------------------------------------
void search()
{
system("cls");
student st;
long int number;
fstream file("a.dat",ios::in|ios::binary|ios::out);
if(!file)
{
cout<<"can not open the file.\n";
getch();
return;
}
cout<<"please enter student number for search:";
cin>>number;
file.read((char*)&st,sizeof(student));
while(!file.eof())
{
if(st.stn==number)
{
cout<<st.fname<<"\t"<<st.lname<<"\t"<<st.ave<<"\t" <<st.stn<<"\n";
file.read((char*)&st,sizeof(student));
break;
}
else
{
cout<<"sorry not found.";
break;
}
}
getch();
file.close();
}
//-----------------------------------------------------------
void delet()
{
system("cls");
long int number1;
int flag=1;
student st;
ifstream in("a.dat",ios::binary|ios::in);
ofstream out("a.dat",ios::binary|ios::out);
if(!in && out)
{
cout<<"can not open the file.\n";
getch();
return;
}
while(flag)
{
cout<<"enter number for search:";
cin>>number1;
in.read((char*)&st,sizeof(student));
if(st.stn==number1)
{
cout<<st.fname<<"\t"<<st.lname<<"\t"<<st.ave<<"\t" <<st.stn<<"\n";
cout<<"enter new information:";
cin>>st.fname>>st.lname>>st.ave>>st.stn;
out.write("a.dat",ios::binary|ios::out);
flag=0;
break;
}
else
{
cout<<"sorry not found.";
break;
}
}
in.close();
out.close();
remove("a.dat");
rename("temp.dat","a.dat");
getch();
ifstream temp("a.dat",ios::in|ios::binary);
cout<<"fname"<<"\t"<<"lname"<<"\t"<<"ave"<<"\t"<<" stn"<<"\n";
temp.read((char*)&st,sizeof(student));
while(!temp.eof())
{
cout<<st.fname<<"\t"<<st.lname<<"\t"<<st.ave<<"\t" <<st.stn<<"\n";
temp.read((char*)&st,sizeof(student));
temp.close();
}
getch();
}
نقل قول: مشکل در delete , edit
سلام.
من فرصت ندارم کد رو کامل بخونم (بخصوص که بسیار هم ناخوانا است) اما با یه نگاه سریع متوجه شدم در خط 168 از کد اول، جای == نوشته اید = ... شاید operator مربوطه رو درست کنید، عمل حذف با موفقیت انجام بشه. در هر حال، اگر خواستید دقیق نگاه کنم، کدتون رو Refactor کنید و در همین تاپیک مجددا ارسال کنید.
موفق باشید.