Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. const int n = 20;
  6.  
  7. struct struct1
  8. {
  9.     int id;
  10.     char name[10];
  11.     double number;
  12.     bool flag;
  13. };
  14.  
  15. void main()
  16. {
  17.     int f1[n];
  18.     double f2[n];
  19.     for (int i = 0; i < n; i++)
  20.         f1[i] = (rand() % 200) - 100;
  21.     ofstream fout1("file1.bin", ios::binary);
  22.     fout1.write((char*)&f1, sizeof(f1));
  23.     fout1.close();
  24.  
  25.     ifstream fin1("file1.bin", ios::binary);
  26.     fin1.read((char*)&f1, sizeof(f1));
  27.     for (int i = 0; i < n; i++)
  28.         f2[i] = f1[i] * 1.0 / ((rand() % 200) - 100);
  29.     ofstream fout2("file2.bin", ios::binary);
  30.     fout2.write((char*)&f2, sizeof(f2));
  31.     fin1.close();
  32.     fout2.close();
  33.  
  34.     ifstream fin2("file2.bin", ios::binary);
  35.     struct1 str[n];
  36.     cout << "Enter " << n << " name:";
  37.     for (int i = 0; i < n; i++)
  38.     {
  39.         cin >> str[i].name;
  40.         str[i].id = i + 1;
  41.         str[i].number = f2[i];
  42.         if (int(f2[i]) == f2[i])
  43.             str[i].flag = true;
  44.         else
  45.             str[i].flag = false;
  46.     }
  47.     ofstream fout3("file3.bin", ios::binary);
  48.     fout3.write((char*)&str, sizeof(str));
  49.     fin2.close();
  50.     fout3.close();
  51.  
  52.     ifstream fin3("file3.bin", ios::binary);
  53.     fin3.read((char*)&str, sizeof(str));
  54.     for (int i = 0; i < n; i++)
  55.     {
  56.         cout.width(5);
  57.         cout << str[i].id;
  58.         cout.width(10);
  59.         cout << str[i].name;
  60.         cout.width(15);
  61.         cout << str[i].number;
  62.         cout.width(10);
  63.         cout << str[i].flag;
  64.         cout << endl;
  65.     }
  66.     fin3.close();
  67.     system("pause");
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement