Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- #define FILE_TXT "StudentDB.txt"
- #define FILE_BIN "StudentDB.dat"
- #define MAX_NAME_LEN 50
- #define MAX_ID_LEN 7
- #define NUM_OF_COURSES 2
- typedef struct
- {
- char id[MAX_ID_LEN] = { 0 };
- char name[MAX_NAME_LEN] = { 0 };
- double course[NUM_OF_COURSES];
- double GPA;
- } Sinhvien;
- Sinhvien *record;
- int num_of_records;
- void readRecord()
- {
- cout << "Number of records: ";
- cin >> num_of_records;
- cin.ignore(255, '\n');
- record = new Sinhvien[num_of_records];
- for (int i = 0; i < num_of_records; i++)
- {
- cout << "=========================" << endl;
- cout << "Record " << i + 1 << endl;
- cout << "ID: ";
- cin.get(record[i].id,MAX_ID_LEN);
- cin.ignore(255, '\n');
- cout << "Name: ";
- cin.get(record[i].name, MAX_NAME_LEN);
- cin.ignore(255, '\n');
- for (int j = 0; j < NUM_OF_COURSES; j++)
- {
- cout << "Course " << j + 1 << ": ";
- cin >> record[i].course[j];
- cin.ignore(255, '\n');
- }
- cout << "GPA: ";
- cin >> record[i].GPA;
- cin.ignore(255, '\n');
- }
- }
- void writeToTxt()
- {
- ofstream fout;
- fout.open(FILE_TXT);
- if (fout.is_open())
- {
- fout << "|";
- fout << setw(MAX_ID_LEN + 3) << left << " ID";
- fout << setw(0) << "|";
- fout << setw(MAX_NAME_LEN + 3) << left << " NAME";
- fout << setw(0) << "|";
- for (int i = 0; i < NUM_OF_COURSES; i++)
- {
- fout << setw(3) << " ";
- fout << setw(0) << "|";
- }
- fout << setw(5) << left << " GPA";
- fout << setw(0) << "|" << endl;
- for (int j = 0; j < num_of_records; j++)
- {
- fout << "|";
- fout << setw(MAX_ID_LEN + 3) << right << record[j].id;
- fout << setw(0) << "| ";
- fout << setw(MAX_NAME_LEN + 2) << left << record[j].name;
- fout << setw(0) << "|";
- for (int i = 0; i < NUM_OF_COURSES; i++)
- {
- fout << setw(3) << right << record[j].course[i];
- fout << setw(0) << "|";
- }
- fout << setw(5) << right << record[j].GPA;
- fout << setw(0) << "|" << endl;
- }
- fout.close();
- }
- }
- void writeToBin()
- {
- ofstream fout;
- fout.open(FILE_BIN, ios::binary);
- if (fout.is_open())
- {
- fout.write(reinterpret_cast<char*>(&num_of_records), 4);
- for (int i = 0; i < num_of_records; i++)
- {
- fout.write(record[i].id,MAX_ID_LEN);
- fout.write(record[i].name, MAX_NAME_LEN);
- for (int j = 0; j < NUM_OF_COURSES; j++)
- {
- fout.write(reinterpret_cast<char*>(&record[i].course[j]), sizeof(double));
- }
- fout.write(reinterpret_cast<char*>(&record[i].GPA), sizeof(double));
- }
- fout.close();
- }
- }
- void readFromBin()
- {
- ifstream fin;
- fin.open(FILE_BIN, ios::binary);
- if (fin.is_open())
- {
- fin.read(reinterpret_cast<char*>(&num_of_records), 4);
- record = new Sinhvien[num_of_records];
- for (int i = 0; i < num_of_records; i++)
- {
- fin.read(record[i].id, MAX_ID_LEN);
- fin.read(record[i].name, MAX_NAME_LEN);
- for (int j = 0; j < NUM_OF_COURSES; j++)
- {
- fin.read(reinterpret_cast<char*>(&record[i].course[j]), sizeof(double));
- }
- fin.read(reinterpret_cast<char*>(&record[i].GPA), sizeof(double));
- }
- cout << "|";
- cout << setw(MAX_ID_LEN + 3) << left << " ID";
- cout << setw(0) << "|";
- cout << setw(MAX_NAME_LEN + 3) << left << " NAME";
- cout << setw(0) << "|";
- for (int i = 0; i < NUM_OF_COURSES; i++)
- {
- cout << setw(3) << " ";
- cout << setw(0) << "|";
- }
- cout << setw(5) << left << " GPA";
- cout << setw(0) << "|" << endl;
- for (int j = 0; j < num_of_records; j++)
- {
- cout << "|";
- cout << setw(MAX_ID_LEN + 3) << right << record[j].id;
- cout << setw(0) << "| ";
- cout << setw(MAX_NAME_LEN + 2) << left << record[j].name;
- cout << setw(0) << "|";
- for (int i = 0; i < NUM_OF_COURSES; i++)
- {
- cout << setw(3) << right << record[j].course[i];
- cout << setw(0) << "|";
- }
- cout << setw(5) << right << record[j].GPA;
- cout << setw(0) << "|" << endl;
- }
- fin.close();
- }
- }
- int main()
- {
- readRecord();
- writeToTxt();
- writeToBin();
- delete[] record;
- record = nullptr;
- readFromBin();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement