Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;
- struct info
- {
- char name[NAME_SIZE];
- int age;
- char address1[ADDR_SIZE];
- char address2[ADDR_SIZE];
- char phone[PHONE_SIZE];
- };
- int main()
- {
- info person;
- char again;
- fstream people("people.dat", ios::out | ios::binary);
- do
- {
- cout << "Enter the following data about a "
- << "person:\n";
- cout << "Name: ";
- cin.getline(person.name, NAME_SIZE);
- cout << "age: ";
- cin >> person.age;
- cin.ignore();
- cout << "Adress line 1: ";
- cin.getline(person.address1, ADDR_SIZE);
- cout << "address line2: ";
- cin.getline(person.address2, ADDR_SIZE);
- cout << "Phone: ";
- cin.getline(person.phone, PHONE_SIZE);
- people.write(reinterpret_cast<char*>(&person), sizeof(person));
- cout << "Do you want to enter another record? ";
- cin >> again;
- cin.ignore();
- } while (again == 'Y' || again == 'y');
- people.close();
- people.open("people.dat", ios::in | ios::binary);
- if (!people)
- {
- cout << "Error: opening file: Program aborted: ";
- return 0;
- }
- cout << "Here are the people in the file: ";
- people.read(reinterpret_cast<char*>(&person), sizeof(person));
- while (!people.eof())
- {
- cout << "Name: ";
- cout<< person.name << endl;
- cout << "Age: ";
- cout << person.age << endl;
- cout << "Address 1: ";
- cout << person.address1 << endl;
- cout << "Adress 2: ";
- cout << person.address2 << endl;
- cout << "Phone: ";
- cout << person.phone << endl;
- cout << "\n Press the enter key to see the next record.\n";
- cin.get(again);
- people.read(reinterpret_cast<char*>(&person), sizeof(person));
- }
- cout << "Thats all the data in the file: ";
- people.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement