Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Option 1: Write a program that will: 1) read an array of records from the keyboa

ID: 3546950 • Letter: O

Question

Option 1: Write a program that will: 1) read an array of records from the keyboard, 2) store this information to a binary file, 3) read from the binary file back to the array of records, 4) store this information to a textfile. Left justify the information for each field. Each record will consist of the following fields:

first name 15 characters last name 15 characters street address 30 characters city 20 characters state 5 characters zip long integer

You may assume a maximum of 20 records.
This assignment is very similar to the program found in Lab 12.3.

Sample Run:

Enter the following information Person

Explanation / Answer

This is working now :



#include <fstream>


#include <iostream>


#include <cctype> // for toupper function


using namespace std;


struct Peopleinfo//__attribute__((packed)) Peopleinfo // declaring a structure


{


char fname[15];


char lname[15];


char staddr[30];


char city[20];


char state[5];


long int zipcode;


};


int main()


{


fstream infofile ("infofile.dat", ios::out | ios::binary);


// defines infofile as an output binary file


Peopleinfo people_rec ; // defines student as a record (struct)


Peopleinfo opeople_rec;


char input; // used to determine if there is more input


do


{


cout << "Enter the following information" << endl;


cout << "First Name: ";


cin.getline(people_rec.fname,15);


cout << "Last Name: ";


cin.getline(people_rec.lname,15);


cout << "Street Address: ";


cin.getline(people_rec. staddr,30);


cout << "City: ";


cin.getline(people_rec.city,20);


cout << "State: ";


cin.getline(people_rec.city,5);


cout << "Zip Code :";


cin >> people_rec.zipcode;


cin.ignore(); // ignore rest of line


// write this record to the file


infofile.write((char *) &people_rec, sizeof(people_rec));


cout << "Enter a Y if you would like to input more data ";


cin >> input;


cin.ignore();


} while (toupper(input) == 'Y');


infofile.close();


ofstream outfile;


outfile.open ("outputfile.txt");


fstream file ("infofile.dat", ios::in| ios::out|ios::binary);



file.read(opeople_rec.fname, sizeof(opeople_rec.fname));

file.read(opeople_rec.lname, sizeof(opeople_rec.lname));

file.read(opeople_rec.staddr, sizeof(opeople_rec.staddr));

file.read(opeople_rec.city, sizeof(opeople_rec.city));

file.read(opeople_rec.state, sizeof(opeople_rec.state));

file.read(reinterpret_cast<char *>(&opeople_rec.zipcode), sizeof(opeople_rec.zipcode));



if(file.is_open())


{


while (!file.eof()) {


cout << "Reading from Binary File";

file.read(opeople_rec.fname, sizeof(opeople_rec.fname));

file.read(opeople_rec.lname, sizeof(opeople_rec.lname));

file.read(opeople_rec.staddr, sizeof(opeople_rec.staddr));

file.read(opeople_rec.city, sizeof(opeople_rec.city));

file.read(opeople_rec.state, sizeof(opeople_rec.state));

file.read(reinterpret_cast<char *>(&opeople_rec.zipcode), sizeof(opeople_rec.zipcode));


outfile<< opeople_rec.fname;


outfile<<opeople_rec.lname;


outfile<<opeople_rec.staddr;


outfile<<opeople_rec.city;


outfile<<opeople_rec.state;


outfile<<opeople_rec.zipcode;

outfile<<" ";


}


file.close();


}


else cout << "Unable to open file";


cout<<"Completed Writing into Text File";




return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote