






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Operations, Input, Data, Output, File, Stream, Implemented, Object, Operations, Simultaneous
Typology: Slides
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Usman Younis
File Stream
Usman Younis
File input and output
Read data from Disk
Input data for processing
Input file stream
Usman Younis
Output file stream
Output data after processing
Write data to disk
General File I/O Steps
Declare a file name variable
Associate the file name variable with theh f l bl h h disk file name
Open the file
Use the file
Close the file
Usman Younis
Close the file
Files and Streams
Ends with anEnds with an endend of file-of-file markermarker
Object is created and a stream is associated with it Communication between program and file/device
0 1 2 3 4 5 8 9 ...
... (^) n- end-of-file marker
6 7
Usman Younis
p g Similar to cin , cout , which are created when
Files and Streams (contd..)
C++ imposes no structure on file Concept of a "record" must be implemented by the programmer To open file create objects Classes ifstream (input only) ofstream (output only) fstream (I/O) Constructors take file name and file-open mode
Usman Younis
ofstream outClientFile( "filename", fileOpenMode ); To attach a file later ofstream outClientFile; outClientFile.open( "filename", fileOpenMode);
Files and Streams (contd..)
Mode Result in Open for reading (default for ifstream) out Open for writing (default for ofstream) ate Start reading or writing at end of file (AT End) app Start writing at end of file (APPend) trunc Truncate file to zero length if it exists (TRUNCate) bi O fil i bi ( ) d
Usman Younis
ofstream outClientFile( "clients.dat", ios::out ); ofstream outClientFile( "clients.dat"); //ofstream opened for output by default
binary Open file in binary (non-text) mode
Output using sequential access file
int account; char name[30]; char ch = 'y'; double balance;
//read account, name and balance from cin, then place in file while (ch == 'y') { cin>>account; cin>>name; cin>>balance; outFile<<account<<" "<<name<<' '<<balance<<endl;
cout<<"Add another (y/n)?";
Usman Younis
(y ) ; cin>>ch; } //ofstream destructor closes file //or you can do it manually outFile.close(); } // end main
Output using sequential access file
outfile.txt
2345 Taz 56789. 2347 Gurp 7896. 6789 Ozzy 8907. 6754 Moss 8765.
Usman Younis
Input using sequential access file
#include <iostream.h> #include <fstream.h> #include <process.h>
void main() { //ifstream constructor opens file ifstream inFile("outfile.txt", ios::in);
if(!inFile) //overloaded! operator {
Usman Younis
cout<<"Unable to open the file"<<endl;
//exit program if unable to create file exit(1); }
Input using sequential access file
int account; char name[30]; char ch = 'y'; double balance;double balance; //read account, name and balance from inFile, then display while (!inFile.eof()) { inFile>>account>>name>>balance;
cout<<account<<" "<<name<<' '<<balance<<endl; } // end while
Usman Younis
} // end while
//ifstream destructor closes file //or you can do it manually inFile.close(); } // end main
Reading data from a sequential
access file
tellg and tellp location = fileObject.tellg()
Usman Younis
Storage in sequential access file
char array takes 8 bytes (1 for each character + null)char array takes 8 bytes (1 for each character + null) int takes fixed number of bytes (4) 123 same size in bytes as 1234567
outFile << number
Usman Younis
Outputs number (int) as a character array Variable number of bytes
Updating sequential access files
Old data 300 John 400 32. Insert new data 300 John Roberts
Usman Younis
300 John 400 32.
300 John Roberts
300 John Roberts
Data gets overwritten