Advertisement
minh1999

file_exercise

Aug 2nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. #define FILE_TXT "StudentDB.txt"
  6. #define FILE_BIN "StudentDB.dat"
  7. #define MAX_NAME_LEN 50
  8. #define MAX_ID_LEN 7
  9. #define NUM_OF_COURSES 2
  10.  
  11. typedef struct
  12. {
  13.     char id[MAX_ID_LEN] = { 0 };
  14.     char name[MAX_NAME_LEN] = { 0 };
  15.     double course[NUM_OF_COURSES];
  16.     double GPA;
  17. } Sinhvien;
  18.  
  19. Sinhvien *record;
  20. int num_of_records;
  21.  
  22. void readRecord()
  23. {
  24.     cout << "Number of records: ";
  25.     cin >> num_of_records;
  26.     cin.ignore(255, '\n');
  27.     record = new Sinhvien[num_of_records];
  28.     for (int i = 0; i < num_of_records; i++)
  29.     {
  30.         cout << "=========================" << endl;
  31.         cout << "Record " << i + 1 << endl;
  32.         cout << "ID: ";
  33.         cin.get(record[i].id,MAX_ID_LEN);
  34.         cin.ignore(255, '\n');
  35.         cout << "Name: ";
  36.         cin.get(record[i].name, MAX_NAME_LEN);
  37.         cin.ignore(255, '\n');
  38.         for (int j = 0; j < NUM_OF_COURSES; j++)
  39.         {
  40.             cout << "Course " << j + 1 << ": ";
  41.             cin >> record[i].course[j];
  42.             cin.ignore(255, '\n');
  43.         }
  44.         cout << "GPA: ";
  45.         cin >> record[i].GPA;
  46.         cin.ignore(255, '\n');
  47.     }
  48. }
  49.  
  50. void writeToTxt()
  51. {
  52.     ofstream fout;
  53.     fout.open(FILE_TXT);
  54.     if (fout.is_open())
  55.     {
  56.         fout << "|";
  57.         fout << setw(MAX_ID_LEN + 3) << left << " ID";
  58.         fout << setw(0) << "|";
  59.         fout << setw(MAX_NAME_LEN + 3) << left << " NAME";
  60.         fout << setw(0) << "|";
  61.         for (int i = 0; i < NUM_OF_COURSES; i++)
  62.         {
  63.             fout << setw(3) << " ";
  64.             fout << setw(0) << "|";
  65.         }
  66.         fout << setw(5) << left << " GPA";
  67.         fout << setw(0) << "|" << endl;
  68.  
  69.         for (int j = 0; j < num_of_records; j++)
  70.         {
  71.             fout << "|";
  72.             fout << setw(MAX_ID_LEN + 3) << right << record[j].id;
  73.             fout << setw(0) << "| ";
  74.             fout << setw(MAX_NAME_LEN + 2) << left << record[j].name;
  75.             fout << setw(0) << "|";
  76.             for (int i = 0; i < NUM_OF_COURSES; i++)
  77.             {
  78.                 fout << setw(3) << right << record[j].course[i];
  79.                 fout << setw(0) << "|";
  80.             }
  81.             fout << setw(5) << right << record[j].GPA;
  82.             fout << setw(0) << "|" << endl;
  83.         }
  84.         fout.close();
  85.     }
  86. }
  87.  
  88. void writeToBin()
  89. {
  90.     ofstream fout;
  91.     fout.open(FILE_BIN, ios::binary);
  92.     if (fout.is_open())
  93.     {
  94.         fout.write(reinterpret_cast<char*>(&num_of_records), 4);
  95.         for (int i = 0; i < num_of_records; i++)
  96.         {
  97.             fout.write(record[i].id,MAX_ID_LEN);
  98.             fout.write(record[i].name, MAX_NAME_LEN);
  99.             for (int j = 0; j < NUM_OF_COURSES; j++)
  100.             {
  101.                 fout.write(reinterpret_cast<char*>(&record[i].course[j]), sizeof(double));
  102.             }
  103.             fout.write(reinterpret_cast<char*>(&record[i].GPA), sizeof(double));
  104.         }
  105.         fout.close();
  106.     }
  107. }
  108.  
  109. void readFromBin()
  110. {
  111.     ifstream fin;
  112.     fin.open(FILE_BIN, ios::binary);
  113.     if (fin.is_open())
  114.     {
  115.         fin.read(reinterpret_cast<char*>(&num_of_records), 4);
  116.         record = new Sinhvien[num_of_records];
  117.         for (int i = 0; i < num_of_records; i++)
  118.         {
  119.             fin.read(record[i].id, MAX_ID_LEN);
  120.             fin.read(record[i].name, MAX_NAME_LEN);
  121.             for (int j = 0; j < NUM_OF_COURSES; j++)
  122.             {
  123.                 fin.read(reinterpret_cast<char*>(&record[i].course[j]), sizeof(double));
  124.             }
  125.             fin.read(reinterpret_cast<char*>(&record[i].GPA), sizeof(double));
  126.         }
  127.  
  128.         cout << "|";
  129.         cout << setw(MAX_ID_LEN + 3) << left << " ID";
  130.         cout << setw(0) << "|";
  131.         cout << setw(MAX_NAME_LEN + 3) << left << " NAME";
  132.         cout << setw(0) << "|";
  133.         for (int i = 0; i < NUM_OF_COURSES; i++)
  134.         {
  135.             cout << setw(3) << " ";
  136.             cout << setw(0) << "|";
  137.         }
  138.         cout << setw(5) << left << " GPA";
  139.         cout << setw(0) << "|" << endl;
  140.  
  141.         for (int j = 0; j < num_of_records; j++)
  142.         {
  143.             cout << "|";
  144.             cout << setw(MAX_ID_LEN + 3) << right << record[j].id;
  145.             cout << setw(0) << "| ";
  146.             cout << setw(MAX_NAME_LEN + 2) << left << record[j].name;
  147.             cout << setw(0) << "|";
  148.             for (int i = 0; i < NUM_OF_COURSES; i++)
  149.             {
  150.                 cout << setw(3) << right << record[j].course[i];
  151.                 cout << setw(0) << "|";
  152.             }
  153.             cout << setw(5) << right << record[j].GPA;
  154.             cout << setw(0) << "|" << endl;
  155.         }
  156.         fin.close();
  157.     }
  158. }
  159.  
  160. int main()
  161. {
  162.     readRecord();
  163.     writeToTxt();
  164.     writeToBin();
  165.     delete[] record;
  166.     record = nullptr;
  167.     readFromBin();
  168.     system("pause");
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement