PDA

View Full Version : سوال: مشکل در delete , edit



a-t-e-f-e-h
سه شنبه 09 خرداد 1391, 10:39 صبح
سلام
ممکنه کمکم کنید؟
من یه برنامه با فایل و 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();
}

a-t-e-f-e-h
سه شنبه 09 خرداد 1391, 10:42 صبح
البته جای 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();
}

mehdi.mousavi
چهارشنبه 10 خرداد 1391, 12:24 عصر
سلام.
من فرصت ندارم کد رو کامل بخونم (بخصوص که بسیار هم ناخوانا است) اما با یه نگاه سریع متوجه شدم در خط 168 از کد اول، جای == نوشته اید = ... شاید operator مربوطه رو درست کنید، عمل حذف با موفقیت انجام بشه. در هر حال، اگر خواستید دقیق نگاه کنم، کدتون رو Refactor کنید و در همین تاپیک مجددا ارسال کنید.

موفق باشید.