Кое-как с учебником набил следующее: | Код | #include <conio.h> #include <stdlib.h> #include <iomanip.h> #include <fstream.h> #include <stdio.h>
struct kng { char avtor [45],nazvanie [45]; double tsena; }; //-----------------------------------// char*fnameL() { char*S=new char [40]; cout<< " Enter file name to load please: "; cin>>S; cout<<"\n File "<<"'"<<S<<"'"<<" contains following information:\n \n"; return S;
} //-----------------------------------// char*fnameS() { char*D=new char [40]; cout<< " Enter file name to save table to please: "; cin>>D; cout<< "\n The following information saved to file "<<D<<":\n \n"; return D;
} //-----------------------------------// char*fnameS2() { char*E=new char [40]; cout<< " Enter file name to save MAX value to please: "; cin>>E; cout<< "\n The following information saved to file "<<E<<":\n \n"; return E;
} //-----------------------------------// void save (fstream & fout, kng y) { fout<<y.avtor<<' '<<y.nazvanie<<' '<<y.tsena; } //-----------------------------------// void save (char fn[], kng x[],int n) { int i; fstream fout; fout.open (fn, ios::out); fout<<n<<' '; for (i=0; i<n; i++) save (fout, x[i]); fout.close(); fout.clear(); } //-----------------------------------// void save2 (fstream & fout, kng tabl) { fout<<tabl.avtor<<' '<<tabl.nazvanie<<' '<<tabl.tsena; } //-----------------------------------// void save2 (char fn[], kng tabl[],int n) { int i,k; int max=tabl[0].tsena; for (i=1; i<n ; i++) { if(tabl[i].tsena>max) { max=tabl[i].tsena; k=i; } }
fstream fout; fout.open (fn, ios::out);
fout <<1<<' '; for (i=0; i<n; i++) save2 (fout, tabl[k]);
fout.close(); fout.clear(); } //-----------------------------------// void load (fstream & fin, kng & y) { fin>>y.avtor>>y.nazvanie>>y.tsena; } //-----------------------------------// void load (char fn[], kng x[], int & n) { int i; fstream fin; fin.open (fn, ios::in); fin>>n; for (i=0; i<n; i++) load (fin, x[i]); fin.close(); }
//-----------------------------------// void enter (kng & x) { int j; cout << " Enter the book AUTHOR please: "; cin >> x.avtor; cout << " Enter the book NAME please: "; cin >> x.nazvanie; cout << " Enter the book PRI$E please: "; cin >> x.tsena; } //-----------------------------------// void print (kng x) { cout << setw (15) << setfill(' ') << x.avtor <<' ' << setw (15) << x.nazvanie <<' ' << setfill ('.') << setw (15) << x.tsena;endl; } //-----------------------------------// void enter (kng y [], int & n) { cout << " Enter the books QUANTITY: "; cin >> n; int i; for (i=0; i<n; i++) enter (y [i]); } //-----------------------------------// void print (kng y [], int n) { int i; for (i=0; i<n; i++) { print (y [i]); cout << endl; } } //-----------------------------------// kng max(kng tabl[],int n) { int i,k; int max=tabl[0].tsena; for (i=1; i<n ; i++) { if(tabl[i].tsena>max) { max=tabl[i].tsena; k=i; } } return tabl[k]; //print (tabl[k]); //cout << endl; } //-----------------------------------// void main () { int n;kng my[40]; do { textbackground (15); textcolor (1); clrscr (); cout << " MAIN MENU::PLEASE MAKE YOUR SELECTION\n \n"; cout << " '1' - Enter the table values,\n '2' - Show the table,\n '3' - Show the most expensive book,\n '4' - Save table to a file,\n '5' - Load table from a file,\n Esc - Quit program.\n ___________________________________\n \n"; switch (getch ()) { case '1': enter (my,n); break; case '2': print (my,n); break; case '3': print (max(my,n)); break; case '4': save (fnameS(),my,n); print (my,n); cout<<" \n"; save2 (fnameS2(),my,n); print (max(my,n)); cout<<" \n"; cout<<"\n Saving complete...Press any key to continue"; break; case '5': load (fnameL(),my,n); print (my,n); cout<<"\n Loading complete...Press any key to continue"; break; case 27: exit (0); } getch (); }while (1); }
|
Вопрос по работе с файлами.  Там при нажатии 4 предлагается сохранить в файл всю введённую таблицу и сразу же в другой файл только часть таблицы с максимальным значением (самой дорогой книжкой).  Бьюсь с тем что при загрузке этих сохранённых файлов при нажатиии 5 они как-то не правильно загружаются  . Правда очень нужна помощь. Помогите, пожалуйста  , буду очнь признателен.
|