Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- struct Unit {
- string Name;
- string FIO;
- int amountOfEmployes;
- int amountOfFreeEmployes;
- };
- int read() {
- ifstream fin, fin_bin;
- string path1 = "test.dat",
- path = "test.txt";
- fin.open(path);
- if (!fin) {
- cout << "Error open file!";
- }
- else {
- cout << "file open!\n";
- char ch;
- while (fin.get(ch)) {
- cout << ch;
- }
- }
- fin.close();
- fin_bin.open(path1, ios::binary);
- if (!fin_bin) {
- cout << "Error open binary file!\n";
- }
- else {
- cout << "Sucessful open binary file!\n";
- fin_bin.seekg(0);
- char ch = 0;
- int n = 0;
- fin_bin.read((char*)&n, sizeof(int));
- for (int i = 0; i < n; i++) {
- string str;
- int number;
- getline(fin_bin, str);
- std::cout << "Name of Unit: " << str << endl;
- getline(fin_bin, str);
- std::cout << "FIO of Chief: " << str << endl;
- fin_bin.read((char*)&number, sizeof(int));
- std::cout << "Number of emoployers: " << number << endl;
- fin_bin.read((char*)&number, sizeof(int));
- std::cout << "Number of free employers: " << number << endl;
- cout << "-------------------------------------\n";
- }
- fin_bin.close();
- }
- system("pause");
- return 0;
- }
- int write() {
- int n;
- ofstream fin, fin_bin;
- string path1 = "test.dat",
- path = "test.txt";
- vector<Unit> array;
- cout << "Enter number of Units: "; cin >> n;
- cin.ignore();
- array.resize(n);
- for (int i = 0; i < n; i++) {
- cout << "Enter information about about " << i+1 << " Unit:\n";
- cout << "Name: "; getline(cin, array[i].Name);
- cout << "FIO: "; getline(cin, array[i].FIO);
- cout << "Amount of employes: "; cin >> array[i].amountOfEmployes;
- cout << "Amount of free employes: "; cin >> array[i].amountOfFreeEmployes;
- cin.ignore();
- cout << endl;
- }
- fin.open(path);
- if (!fin) {
- cout << "Error open file!";
- }
- else {
- cout << "file open!\n";
- for (int i = 0; i < n; i++) {
- fin << array[i].Name << endl;
- fin << array[i].FIO << endl;
- fin << array[i].amountOfEmployes << endl;
- fin << array[i].amountOfFreeEmployes << endl;
- }
- }
- fin.close();
- fin_bin.open(path1, ios::binary);
- if (!fin_bin) {
- cout << "Error open binary file!\n";
- }
- else {
- cout << "Sucessful open binary file!\n";
- fin_bin.write((char *)&n, sizeof(int));
- for (int i = 0; i < n; i++) {
- fin_bin << array[i].Name << endl;
- fin_bin << array[i].FIO << endl;
- fin_bin.write((char*)&array[i].amountOfEmployes, sizeof(int));
- fin_bin.write((char*)&array[i].amountOfFreeEmployes, sizeof(int));
- }
- fin_bin.close();
- }
- system("pause");
- return 0;
- }
- int main() {
- char mode;
- cout << "r/w: " << endl;
- cin >> mode;
- if (mode == 'r') {
- return read();
- }
- return write();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement