STARTING OUT WITH
Class 16 Class 16 HonorsHonors
2
Create records with structures
Divide typical file processing algorithms into functions
Obj
ectiv
esO
bjec
tives
3
Keyboard - enter dataKeyboard - enter data
4
Keyboard - enter dataKeyboard - enter data
memory
5
Keyboard - enter dataKeyboard - enter data
memory
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
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
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 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
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
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
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’);
PersonDavis
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’);
PersonDavis 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’);
PersonDavis 24
Adams 21Davis 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’);
PersonSmith
Adams 21Davis 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’);
PersonSmith 18
Adams 21Davis 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’);
PersonSmith 18
Adams 21Davis 24Smith 18
20
cout << “Do you want to enter another record? ”;cin >> Again;cin.ignore( );
} while (toupper (Again) == ‘Y’);
PersonSmith 18
Adams 21Davis 24Smith 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; }
Adams 21Davis 24Smith 18
P. 731-733
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
37
#include <fstream.h>//Declaration of Invtry structurestruct Invtry
{char Desc[31];int Qty;float Price;
};
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
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
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
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
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
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
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
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
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];
};
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;}
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
STARTING OUT WITH
Conclusion of Conclusion of Class 16Class 16