Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <io.h>
- #include<iomanip>
- using namespace std;
- FILE* fl;
- struct libr
- {
- char name[10];
- char nazv[40];
- int age;
- int sheets;
- }libb;
- void stars();
- void creating();
- void filling(int* counting);
- void reading(int* counting);
- void checking(int* counting);
- void in_file();
- int menu();
- int main()
- {
- int counting = 0;//skolko mi zapisali
- cout << endl << "start" << endl;
- while (true)
- {
- switch (menu())
- {
- cout << "viberite:" << endl;
- case 1:creating();
- break;
- case 2: filling(&counting);
- break;
- case 3: reading(&counting);
- break;
- case 4: checking(&counting);
- break;
- case 5: in_file();
- case 6: return 0;
- default: "Viberete normalniy variant";
- }
- }
- }
- int menu()
- {
- int i, pi = 0;
- cout << "1. Sozdanie" << endl;
- cout << "2. Zapiz" << endl;
- cout << "3. Pokazat'" << endl;
- cout << "4.Proverit'" << endl;
- cout << "5.V fail" << endl;
- cout << "6.Zakonchit;" << endl;
- int k; cin >> k;
- return k;
- }
- void stars()
- {
- for (int i = 0; i < 50; i++)
- {
- cout << "*";
- ++i;
- }
- cout << endl << endl;
- }
- void creating()//sozdanie faila
- {
- if ((fl = fopen("test16.bin", "wb")) == NULL)
- {
- cout << "\nerror with creatin\n";
- }
- else
- {
- cout << endl << "sucs creating" << endl;
- }
- }
- void filling(int* counting)//vvod dannih v fail
- {
- if ((fl = fopen("test16.bin", "ab+")) == NULL)
- {
- cout << "error with writing";
- }
- else
- {
- cout << endl << "File is redy to be filed" << endl << endl;
- char contin[3];
- do
- {
- ++* counting;
- cout << "enter author: ";
- cin >> libb.name;
- cout << "enter nazvanie: ";
- cin >> libb.nazv;
- cout << "age of publish: ";
- cin >> libb.age;//проверка, чтобы год не было 2020+
- while (libb.age > 2020)
- {
- cout << "Error: neverniy god" << endl;
- cout << "age of publish : ";
- cin >> libb.age;
- }
- cout << "How many sheets: ";
- cin >> libb.sheets;
- fwrite(&libb, sizeof(libr), 1, fl); //writing
- cout << endl << endl << "contin? (1/0)" << endl;
- cin >> contin;
- } while (strcmp(contin, "1") == 0);
- }
- cout << endl << "Kol-vo zapisan avtorov: " << *counting << endl;
- fclose(fl);
- }
- void reading(int* counting)//vivod dannih iz faila
- {
- if ((fl = fopen("test16.bin", "rb+")) == NULL)
- {
- cout << "error with reading";
- }
- else
- {
- cout << endl << "file is ready to read" << endl;
- for (int i = 0; i < *counting; i++)
- {
- stars();//funcia to krasiviy vivod
- fread(&libb, sizeof(libr), 1, fl);
- cout << "Athor: " << libb.name << endl;
- cout << "Nazvanie: " << libb.nazv << endl;
- cout << "Year: " << libb.age << endl;
- cout << "Sheets: " << libb.sheets << endl;
- stars();
- }
- }
- fclose(fl);
- }
- void checking(int* counting)
- {
- int year;
- if ((fl = fopen("test16.bin", "rb+")) == NULL)
- {
- cout << "error with reading";
- }
- else
- {
- cout << endl << "Info is ready to be cheaked" << endl;
- cout << "Enter year posle which info should be shown: ";
- cin >> year;
- while (year > 2020)
- {
- cout << endl << "OLO god eshche ne nastupil" << endl;
- cout << "Vvedite normalniy god";
- cin >> year;
- }
- for (int i = 0; i < *counting; i++)
- {
- fread(&libb, sizeof(libr), 1, fl);
- if (libb.age > year)
- {
- stars();
- cout << "Athor: " << libb.name << endl;
- cout << "Nazvanie: " << libb.nazv << endl;
- cout << "Year: " << libb.age << endl;
- cout << "Sheets: " << libb.sheets << endl;
- stars();
- }
- }
- }
- fclose(fl);
- }
- void in_file()
- {
- int year = 0;
- char ft_name[20];
- FILE* ft;
- int n = 0;
- cout << endl << "Vvedite nazvanie faila: ";
- cin >> ft_name;
- if ((ft=fopen(ft_name,"wb"))==NULL)
- {
- cout <<endl<< "error mistake with creating new file" << endl;
- }
- else
- {
- cout << endl << "file secs created" << endl;
- }
- if ((ft = fopen(ft_name, "rb+")) == NULL)
- {
- cout << endl << "error mistake with reading new file" << endl;
- }
- else
- {
- cout << endl << "file secs read" << endl;
- }
- n = filelength(fileno(fl)) / sizeof(libr);
- cout << "Enter year posle which info should be shown: ";
- cin >> year;
- while (year > 2020)
- {
- cout << endl << "OLO god eshche ne nastupil" << endl;
- cout << "Vvedite normalniy god";
- cin >> year;
- }
- for (int i = 0; i < n; i++)
- {
- fread(&libb, sizeof(libr), 1, fl);
- if (libb.age > year)
- {
- fprintf(ft, "%s nazvanie: %d\n", libb.age, libb.nazv);
- }
- }
- fclose(fl);
- fclose(ft);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement