Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;
  6.  
  7. struct info
  8. {
  9.     char name[NAME_SIZE];
  10.     int age;
  11.     char address1[ADDR_SIZE];
  12.     char address2[ADDR_SIZE];
  13.     char phone[PHONE_SIZE];
  14. };
  15.  
  16. int main()
  17. {
  18.     info person;
  19.     char again;
  20.  
  21.     fstream people("people.dat", ios::out | ios::binary);
  22.  
  23.     do
  24.     {
  25.         cout << "Enter the following data about a "
  26.             << "person:\n";
  27.         cout << "Name: ";
  28.         cin.getline(person.name, NAME_SIZE);
  29.         cout << "age: ";
  30.         cin >> person.age;
  31.         cin.ignore();
  32.         cout << "Adress line 1: ";
  33.         cin.getline(person.address1, ADDR_SIZE);
  34.         cout << "address line2: ";
  35.         cin.getline(person.address2, ADDR_SIZE);
  36.         cout << "Phone: ";
  37.         cin.getline(person.phone, PHONE_SIZE);
  38.  
  39.         people.write(reinterpret_cast<char*>(&person), sizeof(person));
  40.  
  41.         cout << "Do you want to enter another record?   ";
  42.         cin >> again;
  43.         cin.ignore();
  44.  
  45.     } while (again == 'Y' || again == 'y');
  46.  
  47.     people.close();
  48.  
  49.  
  50.     people.open("people.dat", ios::in | ios::binary);
  51.     if (!people)
  52.     {
  53.         cout << "Error: opening file: Program aborted: ";
  54.         return 0;
  55.     }
  56.  
  57.     cout << "Here are the people in the file: ";
  58.     people.read(reinterpret_cast<char*>(&person), sizeof(person));
  59.  
  60.     while (!people.eof())
  61.     {
  62.         cout << "Name: ";
  63.         cout<< person.name << endl;
  64.         cout << "Age: ";
  65.         cout << person.age << endl;
  66.         cout << "Address 1: ";
  67.         cout << person.address1 << endl;
  68.         cout << "Adress 2: ";
  69.         cout << person.address2 << endl;
  70.         cout << "Phone: ";
  71.         cout << person.phone << endl;
  72.  
  73.         cout << "\n Press the enter key to see the next record.\n";
  74.         cin.get(again);
  75.         people.read(reinterpret_cast<char*>(&person), sizeof(person));
  76.  
  77.     }
  78.     cout << "Thats all the data in the file: ";
  79.     people.close();
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement