Hi! I have to make a program in C++ using Visual C++ 6.0 .There must be a list of options for operating with the program:
1..Creating a file which holds information about all of the teachers in the university.
2..Showing the information ,written in the file, on the screen.
3..Adding information about a teacher.
4..Deleting information about a teacher
All of the functions work exept for the last one.When I delete the info about a teacher and try to show the new information written in the file it says "Can't read the file".Can you please tell me where my mistake is because I can't find it by myself and I need to have this program done by tuesday.
const N=10;
long begin,end;
struct prep //structure that hold the info about the teachers
{
char ime[10]; //name
char prezime[10]; //surname
char familiq[10]; //family name
int hours;
};
int static m;
fstream fp;
void vyvejdane(struct prep pre[]); //creating a file
void chetene(struct prep pre[]); //reading from the file
void dopylvane(struct prep pre[]); //adding info
void iztrivane(struct prep pre[]); //deleting info
if(izbor<1 || izbor>5)
cout<<endl<<"Greshka pri vyvejdane!!! Molia vyvedete otnovo..."<<endl<<endl<<endl;
}
while(izbor<1 || izbor>4);
switch(izbor)
{
case 1: vyvejdane(pre); break;
case 2: chetene(pre); break;
case 3: dopylvane(pre); break;
case 4: iztrivane(pre); break;
case 5: break;
default: cout<<"Greshka pri vyvejdane!!! Molia vyvedete otnovo..."<<endl;
}
}
while(izbor!=5);
return izbor;
}
void vyvejdane(struct prep pre[])
{
cout<<"Molia vyvedete broia na prepodavatelite"<<endl;
cin>>m; //m-number of teachers
fp.open("my_file.dat",ios::binary|ios::out);
if(!fp)
{ cout<<"Greshka pri syzdavane na faila"<<endl;
exit(1); }
for(int i=0; i<m; i++)
{
fflush(stdin);
cout<<endl<<"VYVEDETE DANNITE ZA PREPODAVATEL "<<i+1<<endl<<endl;
cout<<"vyvedete ime i natisnete ENTER... ";
cin>>pre[i].ime;
cout<<"vyvedete prezime i natisnete ENTER... ";
cin>>pre[i].prezime;
cout<<"vyvedete familiia i natisnete ETER... ";
cin>>pre[i].familiq;
cout<<"vyvedete broiat otrabotenite chasove za sedmica i natisnete ENTER... ";
cin>>pre[i].hours;
};
fp.write((char*)pre,m*sizeof(prep));
fp.close();
}
void chetene(struct prep pre[])
{
int i=0;
struct prep c;
cout<<endl<<"STATUS NA PREPODAWATELITE : "<<endl<<endl<<endl;
fp.open("my_file.dat",ios::binary|ios::in);
if(!fp)
{
cout<<endl<<"Greshka pri prochitane na faila!"<<endl; //error
exit(1);
}
If you want to delete the teacher on nomer, just move to the left the entire list and that way u delete the teacher you want, now you will have the last two the same so, m-- to delete the last one, and ready.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
fp.seekg((nomer)*sizeof(prep));
for(int k=(nomer-1); k<10; k++)
{
pre[k]=pre[k+1];
};
m--;
fp.write((char*)pre, m*sizeof(prep));
cout<<"Dannite sa iztriti!"<<endl<<endl; //"The data is deleted"
fp.close();
void iztrivane(struct prep pre[]) // deleting
{
int nomer;
struct prep f[50];
struct prep t[50];
int en;
int l;
fp.open(LIST, ios::binary|ios::in);
fp.seekg (0l, ios::end); //finds the number of elements in the massiv
end = fp.tellg();
en=(end/sizeof(prep));
cout<<"Molia vyvedete nomer na prepodavatel, chiito danni jelatete da iztriete"<<endl<<endl;
cin>>nomer; // number of a teacher
for(int i=0,s=0; i<(nomer-1); i++) //saving the elements before the one we wanna erase in a new massiv t
{
t[s]=pre[i];
s++; };
for( i=nomer, l=0; i<=en ; i++) //saving the elements after the one we wanna delete in a new massive f
{
f[l]=pre[i];
l++ ; };
fp.seekg((nomer-1),ios::beg);
fp.close();
fp.open(LIST,ios::binary|ios::out);
fp.write((char*)t,(nomer-1)*sizeof(prep)); //saving the elements before the one we wanna erase in the file
fp.write((char*)f,(l+1)*sizeof(prep)); //saving the elements after the one we wanna erase in the file
fp.close();
m--;
cout<<"Dannite sa iztriti!"<<endl<<endl;
}
Now it works OK but ...after I do these steps:
1) erase a teacher
2) add a new one
3)try to read the file again
at the 3d step there's a problem- it openes the file but it seems to be ruined. Any ideas how to fix that?
fp.write((char*)t,(nomer-1)*sizeof(prep)); //saving the elements before the one we wanna erase in the file
fp.write((char*)f,(l+1)*sizeof(prep)); //saving the elements after the one we wanna erase in the file
When you do the last loop the l value is incremented once more, i think that you need l - 1, not add one, try it and let my know,
fp.write((char*)f,(l-1)*sizeof(prep));
by the way, good solution, is a liltle large but still good, :)