А що се отнася за цикъла do-while. Да така е, той е останал, защото имах проблеми с отпечатването на файла. Последния елемент ми го печаташе винаги два пъти. Този проблем го оправих. Ето кода - в него работят create, print и append.
#include <iostream>
#include <fstream>
#include <fcntl.h>
using namespace std;
struct uchenik
{unsigned nom;
char ime[20];
char egn[11];
float usp;
}u;
int is;
/*void append();
void update();
void choose();
void del();
*/
void create ()
{ ofstream f;
f.open("klas.dat",ios::out);
if (!f) {cout<<"Error"<<"\n";
system("pause");
return ;}
int n=-1, is;
while (n!=0)
{cout<<"Nomer: "; cin>>u.nom;
cout<<"Ime: ";cin>>u.ime;
cout<<"EGN: "; cin>>u.egn;
cout<<"Uspeh: "; cin>>u.usp;
f.write((const char*)&u,sizeof(struct uchenik));
cout<<"Nov zapis: 1-da,0-ne"<<"\n";
cin>>n; }
f.close();
}
void print()
{ ifstream f;
f.open("klas.dat",ios::in);
if (!f) {cout<<"Error"<<"\n";
system("pause");
return ;}
while (!f.eof())
{if(!(f.read((char*)&u,sizeof(struct uchenik))))break;
cout<<"Nomer: "<<u.nom<<endl;
cout<<"Ime: "<<u.ime<<endl;
cout<<"EGN: "<<u.egn<<endl;
cout<<"Uspeh: "<<u.usp<<endl<<endl;
}
f.close();
}
void append()
{ ofstream f;
f.open("klas.dat",ios::app);
if (!f) {cout<<"Error"<<"\n";
system("pause");
return ;}
int n=-1;
while (n!=0)
{cout<<"Nomer: "; cin>>u.nom;
cout<<"Ime: ";cin>>u.ime;
cout<<"EGN: "; cin>>u.egn;
cout<<"Uspeh: "; cin>>u.usp;
f.write((const char*)&u,sizeof(struct uchenik));
cout<<"Nov zapis: 1-da,0-ne"<<"\n";
cin>>n; }
f.close();
}
void update()
{unsigned long pos;
fstream f;
f.open("klas.dat",ios::in|ios::ate|ios::binary);
unsigned search_nom;
float ocenka;
cout<<"Vyvedi tyrseniq nomer: ";cin>>search_nom;
cout<<"Vyvedi nova ocenka: ";cin>>ocenka;
if (!f) {cout<<"Error"<<"\n";
system("pause");
return ;}
while (!f.eof())
{f.read((char*)&u,sizeof(struct uchenik));
if(u.nom==search_nom)
{
pos=f.tellp()-sizeof(struct uchenik);
cout << pos << endl;
f.seekp(pos,ios::beg );
u.usp=ocenka;
f.write((const char*)&u,sizeof(struct uchenik));
}
}
f.close();
}
int main()
{ int op=-1;
while (op!=0)
{cout<<"izberete funciq:"<<endl;
cout<<" 1-sazdavane na fail\n"<<endl;
cout<<" 2-izvejdane na fail\n"<<endl;
cout<<" 3-dobavqne na u4enik\n"<<endl;
cout<<" 4-promqna na ocenka\n"<<endl;
cout<<" 5-izbor po uspeh\n"<<endl;
cout<<" 6-iztrivane\n"<<endl;
cout<<"vsi4ko drugo-krai\n"<<endl;
cin>>op;
switch (op)
{ case 1: create();break;
case 2:print();break;
case 3:append();break;
/*case 4:update();break;
case 5:choose();break;
case 6:del();break;*/
default:exit(0); }
}
system("PAUSE");
return 0;
}