CS201 18
CS201 18
Lecture 18
File
Types of Files
Text Files
Executable Programs
Memory is volatile
fstream.h
Header File for File
Handling
#include <fstream.h>
Input File Stream
ifstream
Output file stream
ofstream
Example 1
#include <fstream.h>
ifstream myFile ;
myFile.open ( “payRoll.txt” ) ;
Fully Qualified Path Name
C:\myProg\payRoll.txt
Access file data
myfile >> var1;
myFile.close ( ) ;
Process : Open
myfile.open ( “payRoll.txt” ) ;
myFile payRoll.txt
payRoll.txt
Process: Close
myfile.close ( “payRoll.txt” ) ;
payRoll.txt
payRoll.txt
myFile
X
Example 1
ifstream myFile ;
myFile.open ( “myFile.txt” ) ;
while ( !myfile.eof ( ) )
{
myfile >> varName ;
}
get ( )
char ch ;
myFile.get ( ch ) ;
Example 2
while ( !myFile.eof ( ) )
{
myFile.get ( ch ) ;
cout << ch ;
}
put ( )
outputFile.put ( ch ) ;
ifstream myInputFile ( “myfile.txt” , ios :: in ) ;
int i = 0 ;
Open file
Output
Hello
File
Amir 1000
Amara 1002
strtok ( string , delimiter )
Example
char * namePtr , *salaryPtr , arr [ 30 ] ;
double fSalary = 0.0 ;
inFile.getline ( arr , 30 , ‘\n’ ) ;
namePtr = strtok ( arr , " " ) ;
salaryPtr = strtok ( NULL , " " ) ;
fSalary = atof ( salaryPtr ) ;
: