500 likes | 584 Views
Class 16 Honors. Objectives. Create records with structures Divide typical file processing algorithms into functions. Keyboard - enter data. Keyboard - enter data. memory. Keyboard - enter data. memory. Creating Records with Structures. P. 731-733. #include <fstream.>
E N D
Objectives • Create records with structures • Divide typical file processing algorithms into functions
Keyboard - enter data memory
Keyboard - enter data memory
Creating Records with Structures P. 731-733 #include <fstream.> #include <cctype> // for toupper // Modified from textbook //Declare a structure for a record. struct Info { char Name[15]; int age; };
Creating Records with Structures P. 731-733 int main() { fstream Outfile(“people.dat”, ios::out | ios::binary); Info P; char Again; if (Outfile ==0) { cout << “Error opening file. Program aborting.\n; return; } GetData(P,Outfile); }
void GetData(Info &Person,fstream &People) {do { char buffer[80]; cout << “Enter the persons name; cin.getline(Person.Name, 15); do { flag =0; cout << “Age: ”; cin.getline(buffer, 4); for (int index=0; index<strlen(buffer); index++) if (!isdigit(buffer[index])) { flag=1; cout << “invalid data entered”; break; } } while (flag == 1); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Info)); Person People
cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); } // end of function
void GetData(Info &Person,fstream &People) { char buffer[4]; do { cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); // validate function would need to be definedPerson.age=atoi(buffer); People.write((char *)&Person, sizeof (Info)); cout << “Do you want to “ << “enter another record?”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); } Person People
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer));Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Adams
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Adams 21
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Adams 21 Adams 21
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Davis Adams 21
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Davis 24 Adams 21
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof(Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Davis 24 Adams 21 Davis 24
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith Adams 21 Davis 24
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith 18 Adams 21 Davis 24
cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith 18 Adams 21 Davis 24 Smith 18
cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith 18 Adams 21 Davis 24 Smith 18
struct Info // Modified from textbook { char Name[15]; int age; }; int main() { fstream People; char Again; People.open(“people.dat”, ios::in | ios::binary); if (People == 0) { cout << “Error opening file. Program aborting.\.”; return; } cout << “Here are the “ << “people in the file:\n\n”;ReadFile(People); return 0; } P. 731-733 Adams 21 Davis 24 Smith 18
void ReadFile(fstream &P) { Info Person;P.read((char *) &Person, sizeof(Info)); while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person));} cout << “That’s all the” << “ information in the file!\n; P.close( ); } Adams 21 Davis 24 Smith 18
while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)); } cout << “That’s all the information” << “in the file!\n;” P.close( ); } Adams 21 Davis 24 Smith 18
while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)); } cout << “That’s all the information” << “in the file!\n”; P.close( ); } Adams 21 Davis 24 Smith 18
while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)); } cout << “That’s all the information” << “ in the file!\n”; P.close( ); } Adams 21 Davis 24 Smith 18
while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)) } cout << “That’s all the information” <<“ in the file!\n”; P.close( ); } Adams 21 Davis 24 Smith 18
int main() // Using an array { fstream People; char Again;int count = 0; Info Persons[20]; People.open(“people.dat”, ios::in | ios::binary); if (People == 0) {cout << “Error opening file.” << “Program aborting.\.”; return; } cout << “Here are the “ << “people in the file:\n\n”;ReadFile(People,Persons,count); } Adams 21 Davis 24 Smith 18
void ReadFile(fstream &P, Info a[], int &count) { P.read((char *)&a[count], sizeof(Info)); while (!P.eof( )) { count++;P.read((char *)&a[count], sizeof(Info)); } P.close( ); } Adams 21 Davis 24 Smith 18
Creating a Random access File P. 736 • File.seekp(32L, ios::beg); • Sets the write position to the 33rd byte (byte 32) from the beginning of the file. • File.seekp(-10L, ios::end); • Sets the write position to the 11th byte (byte 10) from the end of the file.
Creating a Random access File P. 736 • File.seekp(120L, ios::curr) • Sets the write position to the 121st byte (byte 120) from the current position • File.seekg(2L, ios::beg); • Sets the read position to the 3rd byte (byte 2) from the beginning of the file.
Creating a Random access File P. 736 • File.seekg(-100L, ios::end); • Sets the read position to the 101st byte (byte 100) from the end of the file. • File.seekg(40L, ios::cur); • Sets the read position to the 41st byte (byte 40) from the current position.
Creating a Random access File P. 736 • File.seekg(0L, ios::end); • Sets the read position to the end of the file.
#include <fstream> #include <iostream> using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } P. 737Pgrm 12-17 abcdefghijklm
#include <fstream> #include <iostream> using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } P. 737Pgrm 12-17 abcdefghijklm
#include <fstream> #include <iostream> using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } P. 737Pgrm 12-17 abcdefghijklm
#include <fstream> #include <iostream> using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } P. 737Pgrm 12-17 abcdefghijklm
#include <fstream.h> //Declaration of Invtry structure struct Invtry { char Desc[31]; int Qty; float Price; };
P. 744Pgrm 12-20 int main() { fstream Inventory(“Invtry.dat”, ios::out | ios::binary); Invtry Record = { “ ”, 0, 0.0 }; //Now write the blank records for (int Count = 0; Count < 5; Count++) { cout << “Now writing record” << Count << endl; Inventory.write((char *)&Record, sizeof(Record)); } Inventory.close( ); return 0; } \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0
P. 746Pgrm 12-22 int main() { fstream Inventory(“Invtry.dat”, ios::in | ios::out | ios::binary); Invtry Record; long RecNum; cout <<“Which record do you want” <<“to edit?”; cin >> RecNum; Inventory.seekg(RecNum * sizeof (Record), ios::beg); Inventory.read((char *) &Record, sizeof(Record)); \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 Record
P. 747Pgrm 12-22 cout << “Description: ”; cout << Record.Desc << endl; cout << “Quantity: ”; cout << Record.Qty << endl; cout << Price: ”; cout << Record.Price << endl; \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0
P. 747Pgrm 12-22 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *)&Record, sizeof(Record)); Inventory.close( ); return 0; } \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \0 0 0.0 Record
P. 747Pgrm 12-22 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *)&Record, sizeof(Record)); Inventory.close( ); return 0; } \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 TV 0 0.0 Record
P. 747Pgrm 12-22 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *)&Record, sizeof(Record)); Inventory.close( ); return 0; } \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 TV 2 0.0 Record
P. 747Pgrm 12-22 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *)&Record, sizeof(Record)); Inventory.close( ); return 0; } \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 \ 0 0 0.0 TV 2 400.00 Record
P. 747Pgrm 12-22 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *)&Record, sizeof(Record)); Inventory.close( ); return 0; } \ 0 0 0.0 \ 0 0 0.0 TV 2 400.00 \ 0 0 0.0 TV 2 400.00 Record
File Operations #include <fstream> //Declare a structure for the record. struct Info { char Name[51]; int Age; char Address1[51]; char Address2[51]; char Phone[14]; };
File Operations using printer int main() { ofstream Printer(“prn”); fstream People(“people.dat”, ios::in); Info Person; char Again; if (People == 0) { cout << Error opening file. Program aborting.\n”; return; }
People.read((char *)&Person, sizeof(Person)); while (!People.eof( )) { Printer << Person.Name << endl; Printer << Person.Age << endl; Printer << Person.Address1 << endl; Printer << Person.Address2 << endl; Printer << Person.Phone << endl << endl; People.read((char *)&Person, sizeof(Person));} People.close( ); return 0; }