starting out with starting out with class 16 honors

50
STARTING OUT WITH Class 16 Class 16 Honors Honors

Upload: franklin-edwards

Post on 06-Jan-2018

256 views

Category:

Documents


8 download

DESCRIPTION

3 Keyboard - enter data

TRANSCRIPT

Page 1: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

STARTING OUT WITH

Class 16 Class 16 HonorsHonors

Page 2: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

2

Create records with structures

Divide typical file processing algorithms into functions

Obj

ectiv

esO

bjec

tives

Page 3: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

3

Keyboard - enter dataKeyboard - enter data

Page 4: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

4

Keyboard - enter dataKeyboard - enter data

memory

Page 5: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

5

Keyboard - enter dataKeyboard - enter data

memory

Page 6: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

6

Creating Records with StructuresCreating Records with Structures

#include <fstream.>#include <cctype> // for toupper// Modified from textbook//Declare a structure for a record.struct Info{

char Name[15];int age;

};

P. 731-733

Page 7: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

7

Creating Records with StructuresCreating Records with Structuresint 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);}

P. 731-733

Page 8: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

Page 9: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

9

cout << “Do you want to enter another record? ”;

cin >> Again;cin.ignore( );

} while (toupper (Again) == ‘Y’);

} // end of function

Page 10: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 defined

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

Page 11: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonAdams

Page 12: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonAdams 21

Page 13: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

Page 14: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonDavis

Adams 21

Page 15: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonDavis 24

Adams 21

Page 16: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonDavis 24

Adams 21Davis 24

Page 17: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonSmith

Adams 21Davis 24

Page 18: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonSmith 18

Adams 21Davis 24

Page 19: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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’);

PersonSmith 18

Adams 21Davis 24Smith 18

Page 20: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

20

cout << “Do you want to enter another record? ”;cin >> Again;cin.ignore( );

} while (toupper (Again) == ‘Y’);

PersonSmith 18

Adams 21Davis 24Smith 18

Page 21: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

Adams 21Davis 24Smith 18

P. 731-733

Page 22: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 23: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 24: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 25: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 26: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 27: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 28: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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 21Davis 24Smith 18

Page 29: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

29

Creating a Random access FileCreating a Random access FileFile.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.

P. 736

Page 30: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

30

Creating a Random access FileCreating a Random access FileFile.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.

P. 736

Page 31: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

31

Creating a Random access FileCreating a Random access FileFile.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.

P. 736

Page 32: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

32

Creating a Random access FileCreating a Random access File

File.seekg(0L, ios::end);– Sets the read position to the end of

the file.

P. 736

Page 33: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

abcdefghijklm

P. 737Pgrm 12-17

Page 34: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

abcdefghijklm

P. 737Pgrm 12-17

Page 35: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

abcdefghijklm

P. 737Pgrm 12-17

Page 36: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

abcdefghijklm

P. 737Pgrm 12-17

Page 37: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

37

#include <fstream.h>//Declaration of Invtry structurestruct Invtry

{char Desc[31];int Qty;float Price;

};

Page 38: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

38

int main(){ fstream Inventory(“Invtry.dat”,

ios::out | ios::binary);Invtry Record = { “ ”, 0, 0.0 };

//Now write the blank recordsfor (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. 744Pgrm 12-20

Page 39: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

39

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

P. 746Pgrm 12-22

Record

Page 40: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

40

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

Page 41: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

41

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

P. 747Pgrm 12-22

Record

Page 42: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

42

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.0TV 0 0.0

P. 747Pgrm 12-22

Record

Page 43: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

43

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.0TV 2 0.0

P. 747Pgrm 12-22

Record

Page 44: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

44

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.0TV 2 400.00

P. 747Pgrm 12-22

Record

Page 45: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

45

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.0TV 2 400.00\ 0 0 0.0TV 2 400.00

P. 747Pgrm 12-22

Record

Page 46: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

46

File OperationsFile 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];

};

Page 47: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

47

File Operations using printerFile Operations using printerint 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;}

Page 48: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

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

Page 49: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

49

Q & A

Page 50: STARTING OUT WITH STARTING OUT WITH Class 16 Honors

STARTING OUT WITH

Conclusion of Conclusion of Class 16Class 16