1 / 50

Class 16 Honors

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.>

deo
Download Presentation

Class 16 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 16 Honors

  2. Objectives • Create records with structures • Divide typical file processing algorithms into functions

  3. Keyboard - enter data

  4. Keyboard - enter data memory

  5. Keyboard - enter data memory

  6. 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; };

  7. 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); }

  8. 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

  9. cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); } // end of function

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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.

  30. 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.

  31. 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.

  32. Creating a Random access File P. 736 • File.seekg(0L, ios::end); • Sets the read position to the end of the file.

  33. #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

  34. #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

  35. #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

  36. #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

  37. #include <fstream.h> //Declaration of Invtry structure struct Invtry { char Desc[31]; int Qty; float Price; };

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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]; };

  47. 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; }

  48. 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; }

  49. Q & A

  50. Conclusion of Class 16

More Related