Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <clocale>
- using namespace std;
- struct Knigi
- {
- int nomer;
- char avtor[15];
- char nazvanie[15];
- int god;
- int polka;
- };
- char X[15];
- void outfile(fstream& f, int n) //функция вывода на экран содержимого файла
- {
- Knigi a;
- f.seekg(0); //установить указатель файла на начало
- cout<<setw(8)<<"NUMBER|"<<setw(15)<<"AUTHOR|"<<setw(15)<<"NAME|"<<setw(8)<<"YAER"<<setw(8)<<"STACK|"<<endl;
- cout<<"_________________________________________________________"<<endl;
- for(int i=0; i<n; i++)
- {
- f.read((char *)&a,sizeof a); //чтение из файла
- cout<<setw(8)<<a.nomer<<setw(15)<<a.avtor<<setw(15)<<a.nazvanie<<setw(8)<<a.god<<setw(8)<<a.polka;
- cout<<endl;
- }
- cout<<endl;
- }
- void sort(fstream& f, int n) //функция сортировки по фамиилии авторов
- {
- Knigi min,a;
- int n_min;
- for(int i=0; i<n; i++)
- {
- f.seekg(i*(sizeof a));
- f.read((char *)&min,sizeof min);
- n_min=i;
- for(int j=i+1;j<n;j++)
- {
- f.read((char *)&a,sizeof a);
- if(strcmp(a.avtor,min.avtor)<0)
- {
- min=a;
- n_min=j;
- }
- }
- f.seekg(i*(sizeof a));
- f.read((char *) &a,sizeof a);
- f.seekp(i*(sizeof a));
- f.write((char *) &min,sizeof min);
- f.seekp(n_min*(sizeof a));
- f.write((char *) &a,sizeof a);
- }
- }
- void outfileX(fstream& f, int n, char* X)
- {
- Knigi a;
- f.seekg(0);
- cout<<setw(8)<<"NUMBER|"<<setw(15)<<"AUTHOR|"<<setw(15)<<"NAME|"<<setw(8)<<"YAER"<<setw(8)<<"STACK|"<<endl;
- cout<<"_________________________________________________________"<<endl;
- for(int i=0; i<n; i++)
- {
- f.read((char *)&a,sizeof a);
- if(strcmp(a.avtor, X)!=0) continue;
- cout<<setw(8)<<a.nomer<<setw(15)<<a.avtor<<setw(15)<<a.nazvanie<<setw(8)<<a.god<<setw(8)<<a.polka;
- cout<<endl;
- }
- cout<<endl;
- }
- int read_f(fstream& f, fstream& out)
- {
- int i=0;Knigi a;
- while(!f.eof())
- {
- f>>a.nomer>>a.avtor>>a.nazvanie>>a.god>>a.polka;
- out.write((char *) &a, sizeof a);
- i++;
- }
- return i;
- }
- int main()
- {
- setlocale(LC_CTYPE, "Russian");
- fstream in("Knigi.txt", ios::in);
- if(!in)
- {
- cout<<"error opening data.txt"<<endl;
- return 1;
- }
- fstream out("Knigi.dat", ios::out | ios::binary);
- if(!out)
- {
- cout<<"error opening data.dat"<<endl;
- return 1;
- }
- int size_f=read_f(in,out);
- in.close();out.close();
- fstream out_f("Knigi.dat", ios::in | ios::out | ios::binary);
- if(!out_f)
- {
- cout<<"error opening data.dat"<<endl;
- return 1;
- }
- outfile(out_f,size_f);
- sort(out_f,size_f);
- outfile(out_f,size_f);
- //out_f.close();
- cout<<"INPUT X:";
- cin>>X;
- outfileX(out_f,size_f, X);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement