File Processing 1 n The Data Hierarchy From















- Slides: 15
File Processing 1
n The Data Hierarchy From smallest to largest (continued) q Field: group of characters with some meaning n q Record: group of related fields n n n q In payroll system, could be name, SS#, address, wage Each field associated with same employee Record key: field used to uniquely identify record File: group of related records n n q Your name Payroll for entire company Sequential file: records stored by key Database: group of related files n Payroll, accounts-receivable, inventory… 2
Files and Streams n C++ views file as sequence of bytes q Ends with end-of-file marker 0 1 2 3 4 5 6 7 8 9 . . . n n-1 end-of-file marker When file opened q q Object created, stream associated with it cin, cout, etc. created when <iostream> included 3
n Files and Streams To perform file processing q Include <iostream> and <fstream> 4
Creating a Sequential-Access File n C++ imposes no structure on file q n Concept of "record" must be implemented by programmer To open file, create objects q q Creates "line of communication" from object to file Classes n n n q ifstream (input only) ofstream (output only) fstream (I/O) Constructors take file name and file-open mode ofstream out. Client. File( "filename", file. Open. Mode ); q To attach a file later ofstream out. Client. File; out. Client. File. open( "filename", file. Open. Mode); 5
Creating a Sequential-Access File n File-open modes q ofstream opened for output by default n n ofstream out. Client. File( "clients. dat", ios: : out ); ofstream out. Client. File( "clients. dat"); 6
Creating a Sequential-Access File n Operations q Writing to file (just like cout) n q out. Client. File << my. Variable Closing file n out. Client. File. close() 7
1 // Fig. 14. 4: fig 14_04. cpp 2 // Create a sequential file. 3 #include <iostream> 4 9 10 #include <fstream> 11 using std: : ofstream; 12 13 #include <cstdlib> // exit prototype 14 15 int main() { 16 // ofstream constructor opens file 17 ofstream out. Client. File( "clients. dat", ios: : out ); 18 8
19 24 25 cout << "Enter the account, name, and balance. " << endl 26 << "Enter end-of-file to end input. n? "; 27 28 int account; 29 char name[ 30 ]; 30 double balance; 31 // read account, name and balance from cin, then place in file 32 while ( cin >> account >> name >> balance ) { 33 out. Client. File << account << ' ' << name << ' ' << balance 34 << endl; 35 cout << "? "; 36 } // end while 37 return 0; // ofstream destructor closes file 38 } // end main 9
Enter ? 100 ? 200 ? 300 ? 400 ? 500 ? ^Z the account, name, and balance. end-of-file to end input. Jones 24. 98 Doe 345. 67 White 0. 00 Stone -42. 16 Rich 224. 62 10
Example 1: //just writing to a file #include <fstream> #include <iomanip> using namespace std; void main () { ofstream out; out. open("temp. dat", ios: : out); out<<"hello, just learning about files n"; out<<setw(10)<<15<<setw(20)<<"Jordan Univ"<<endl; out. close(); } 11
Example 2: //file input. txt contain 10 marks #include <iostream> #include <fstream> using namespace std; void main () { ifstream infile; infile. open("input. txt"); //(1) marks min=101 max=-1 //(2) max=min= 1 st data item int sum=0, avg, min, max, value, count; cout<<"the values are: n"; infile >> value; cout<<value<<endl; min = value; max = value; sum = value; count = 1; 12
while (!infile. eof()) { infile>>value; cout<<value<<endl; count++; sum = sum + value; if (value < min) min = value; if (value > max) max = value; } avg = sum/count; infile. close(); //write results cout<<"number of values is " << count<< endl; cout<<"sum of values is " <<sum<<endl; cout<<"average of values " <<avg<<endl; cout<<" maximum of value "<<max<<endl; cout<<" minimum of value " <<min<<endl; 13
Example 3: //file input. txt store marks and names for three students #include <iostream> #include <fstream> using namespace std; void main () { ifstream infile; infile. open("input. txt"); //(1) marks min=101 max=-1 //(2) max=min= 1 st data item int sum=0, avg, min, max, value, count; string name; cout<<"the values are: n"; infile >> value>> name; cout<<value<< name<< endl; min = value; max = value; sum = value; count = 1; 14
while (!infile. eof()) { infile>>value>>name; cout<<value<<name<<endl; count++; sum = sum + value; if (value < min) min = value; if (value > max) max = value; } avg = sum/count; infile. close(); //write results cout<<"number of values is " << count<< endl; cout<<"sum of values is " <<sum<<endl; cout<<"average of values " <<avg<<endl; cout<<" maximum of value "<<max<<endl; cout<<" minimum of value " <<min<<endl; 15