#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
using namespace std;
FILE* bfl;
FILE* tfl;
struct Vegetables
{
char name[20];
int month1;
int month2;
int month3;
}*spisok, vegetable_s;
char fname[20] = "";
char fnamet[20] = "";
int n = 0;
void create_file();
void write_in_file();
void open_and_read();
void output_to_the_screen();
void output_in_the_text_file();
int menu();
char* nnf();
int main()
{
setlocale(LC_ALL, "rus");
while (1)
{
switch (menu())
{
case 1: create_file(); break;
case 2: write_in_file(); break;
case 3: open_and_read(); break;
case 4: output_to_the_screen(); break;
case 5: output_in_the_text_file(); break;
case 6: return 0;
default: cout << "Error. Please choose correctly!";
}
puts("Press any key to continue");
system("pause");
system("cls");
}
}
int menu()
{
setlocale(LC_ALL, "rus");
cout << "Choose 1-6" << endl;
cout << "1 - Create file" << endl;
cout << "2 - Write data in the file" << endl;
cout << "3 - Open file and read " << endl;
cout << "4 - Output data from the file on the screen" << endl;
cout << "5 - Output data from the file in the text file" << endl;
cout << "6 - EXIT" << endl;
int i;
cout << endl;
cout << ">>> ";
cin >> i;
system("cls");
return i;
}
char* nnf()
{
setlocale(LC_ALL, "rus");
cout << "Input the name of the file: ";
cin >> fname;
system("cls");
return fname;
}
void create_file()
{
setlocale(LC_ALL, "rus");
if ((bfl = fopen(nnf(), "wb")) == NULL)
{
cout << "Error. File wasn't created" << endl;
return;
}
system("cls");
cout << "File was created!" << endl;
cout << endl;
}
int i = 0;
void write_in_file()
{
setlocale(LC_ALL, "rus");
char ch;
if ((bfl = fopen(nnf(), "ab")) == NULL)
{
cout << "Error. File wasn't opened" << endl;
return;
}
system("cls");
do
{
cout << "Vegetable" << i + 1 << ": ";
cin >> vegetable_s.name;
cout << "1st month: ";
cin >> vegetable_s.month1;
cout << "2nd month: ";
cin >> vegetable_s.month2;
cout << "3rd month: ";
cin >> vegetable_s.month3;
fwrite(&vegetable_s, sizeof(Vegetables), 1, bfl);
cout << endl << "Do you want to continue? (y/n)";
cin >> ch;
cout << endl;
system("cls");
i++;
} while (ch == 'y');
fclose(bfl);
}
void open_and_read()
{
setlocale(LC_ALL, "rus");
if ((bfl = fopen(nnf(), "rb")) == NULL)
{
cout << "Error. File wasn't opened" << endl;
return;
}
n = _filelength(_fileno(bfl)) / sizeof(Vegetables);
spisok = new Vegetables[n];
fread(spisok, sizeof(Vegetables), n, bfl);
cout << endl;
for (int i = 0; i < n; i++)
{
cout << spisok[i].name << ": " << endl;
cout << "1st month: " << spisok[i].month1 << endl;
cout << "2nd month: " << spisok[i].month2 << endl;
cout << "3rd month: " << spisok[i].month3 << endl;
cout << endl;
}
cout << endl;
delete []spisok;
fclose(bfl);
}
void output_to_the_screen()
{
setlocale(LC_ALL, "rus");
if ((bfl = fopen(nnf(), "rb")) == NULL)
{
cout << "Error. File wasn't opened" << endl;
return;
}
cout << "Vegetables that are harvested in August: " << endl;
n = _filelength(_fileno(bfl)) / sizeof(Vegetables);
spisok = new Vegetables[n];
fread(spisok, sizeof(Vegetables), n, bfl);
cout << endl;
Vegetables tmp;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (spisok[i].month3 == 8 && spisok[j].month3 == 8 && strcmp(spisok[i].name, spisok[j].name) == 1)
{
tmp = spisok[i];
spisok[i] = spisok[j];
spisok[j] = tmp;
}
for (int i = 0; i < n; i++)
if (spisok[i].month3 == 8)
cout << spisok[i].name << endl;
cout << endl;
delete[]spisok;
fclose(bfl);
}
void output_in_the_text_file()
{
setlocale(LC_ALL, "rus");
cout << "Input text file name: ";
cin >> fnamet;
if ((tfl = fopen(fnamet, "w")) == NULL)
{
cout << "Error. Text file wasn't created" << endl;
return;
}
if ((bfl = fopen(fnamet, "rb")) == NULL)
{
cout << "Error. File wasn't opened" << endl;
return;
}
n = _filelength(_fileno(bfl)) / sizeof(Vegetables);
spisok = new Vegetables[n];
Vegetables tmp;
fread(&vegetable_s, sizeof(Vegetables), 1, bfl);
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (spisok[i].month3 == 8 && spisok[j].month3 == 8 && strcmp(spisok[i].name, spisok[j].name) == 1)
{
tmp = spisok[i];
spisok[i] = spisok[j];
spisok[j] = tmp;
}
for (int i = 0; i < n; i++)
{
if (spisok[i].month3 == 8)
{
fprintf(tfl, "%s", spisok[i].name);
cout << endl;
}
}
cout << endl;
fclose(bfl);
fclose(tfl);
}