0% found this document useful (0 votes)
30 views37 pages

CS201 18

This document discusses file handling in C++ programming. It covers opening, reading, and closing text and binary files using file streams. The key points are: - Text and binary files can be opened for reading and writing using ifstream, ofstream, and fstream objects. - Functions like open(), close(), get(), put(), eof(), getline() are used to access file contents. - Modes like ios::in, ios::out, ios::app specify how the file will be opened. - Files must be opened before reading or writing, and closed after to prevent errors and leaks.

Uploaded by

bc200403674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views37 pages

CS201 18

This document discusses file handling in C++ programming. It covers opening, reading, and closing text and binary files using file streams. The key points are: - Text and binary files can be opened for reading and writing using ifstream, ofstream, and fstream objects. - Functions like open(), close(), get(), put(), eof(), getline() are used to access file contents. - Modes like ios::in, ios::out, ios::app specify how the file will be opened. - Files must be opened before reading or writing, and closed after to prevent errors and leaks.

Uploaded by

bc200403674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Introduction to Programming

Lecture 18
File
Types of Files
 Text Files

 Executable Programs
 Memory is volatile

 Any data that you key in by keyboard


while a program is running is also
volatile
File Handling

 Text files handling

 Binary files handling


Steps to handle file
 Open the File

 Read / Write the File

 Close the File


Streams
Header File for File
Handling

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;

We can also write:


myfile >> var1 >> var2 ;
Close the File

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

if ( !myFile ) // Error check


{
cout << “Your file could not be
opened”;
}
------
myFile.close ( ) ;
Output File Modes
 Create a new file
 Overwrite an existing file
 Append some text
 Randomly accessing a file
Syntax
fStream fileVar ( “fileName” , mode ) ; // Generic syntax
Opening
Mode

ifstream myfile ( “myfile.txt” , ios :: in ) ;


Opening
Mode

ofstream myfile ( “myfile.txt” , ios :: out ) ;


List of File Handling Modes

ios :: in open for reading (default for ifstream)


ios :: out open for writing (default for ofstream)
ios :: app start writing at end of file (APPend)
ios :: ate start reading or writing at EOF of file (ATEnd)
ios :: trunc truncate file to zero length if it exists (TRUNCate)
ios :: nocreate error when opening if file does not already exist
ios :: noreplace error when opening for output if file already exists
ios :: binary open file in binary (not text) mode
 Append
ofstream myfile (“myfile.txt” , ios :: app ) ;
 Random Access
ofstream myfile ( “myfile.txt” , ios :: ate ) ;
 Truncate
ofstream myfile ( “myfile.txt” , ios::trunc ) ;
myfile.eof ( )

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

ofstream myOnputFile ( “myfile.txt” , ios :: out ) ;


int i ;
i=0;

int i = 0 ;
Open file

ifstream myInputFile ( “myfile.txt” ) ;


Open file

ofstream myOnputFile ( “myfile.txt” ) ;


strtok ( )
getline ( ) function
Syntax of getline function
Numbers of
Character characters to
array be read

myfile.getline (char *s, int n, char delim);


delimiter
Syntax of getline function
Input
Hello World

myfile.getline ( arrayString , 20 , ’W’ ) ;

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

You might also like