0% found this document useful (0 votes)
24 views10 pages

Week 13

The document provides an overview of file handling in C++, explaining that files are treated as sequences of bytes and require specific header files for processing. It details how to open and close files, the various file open modes, and the use of file position pointers for reading and writing data. Additionally, a sample C++ program demonstrates how to create, write to, and read from a file.

Uploaded by

surendran.phd.it
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)
24 views10 pages

Week 13

The document provides an overview of file handling in C++, explaining that files are treated as sequences of bytes and require specific header files for processing. It details how to open and close files, the various file open modes, and the use of file position pointers for reading and writing data. Additionally, a sample C++ program demonstrates how to create, write to, and read from a file.

Uploaded by

surendran.phd.it
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/ 10

WEEK 13

File handling in C++

1
C++ Files and Streams
C++ views each files as a sequence of bytes.
Each file ends with an end-of-file marker.
When a file is opened, an object is created and
a stream is associated with the object.
To perform file processing in C++, the header
files <iostream.h> and <fstream.h> must be
included.
<fstream.> includes <ifstream> and
<ofstream>
2
How to open a file in C++ ?

Ofstream outClientFile(“clients.dat”, ios:out)


OR
Ofstream outClientFile;
outClientFile.open(“clients.dat”, ios:out)

3
File Open Modes
ios:: app - (append) write all output to the end of
file
ios:: ate - data can be written anywhere in the file
ios:: binary - read/write data in binary format
ios:: in - (input) open a file for input
ios::out - (output) open a file for output
ios: trunc -(truncate) discard the files’ contents if
it exists 4
How to close a file in C++?
The file is closed implicitly when a
destructor for the corresponding object is
called
OR
by using member function close:
outClientFile.close();

5
File position pointer
<istream> and <ostream> classes provide
member functions for repositioning the file
pointer (the byte number of the next byte in the
file to be read or to be written.)
These member functions are:
seekg (seek get) for istream class
seekp (seek put) for ostream class

CPSC 231 D.H. C++ File 6


Processing
Examples of moving a file
pointer
inClientFile.seekg(0) - repositions the file get pointer to
the beginning of the file
inClientFile.seekg(n, ios:beg) - repositions the file get
pointer to the n-th byte of the file
inClientFile.seekg(m, ios:end) -repositions the file get
pointer to the m-th byte from the end
of file
nClientFile.seekg(0, ios:end) - repositions the file get
pointer to the end of the file
The same operations can be performed with <ostream>
function member seekp.
7
Sample file handling program
//C++ program to write and read text in/from file.
#include <iostream>
#include <fstream>
int main()
{
fstream file; //object of fstream class
//opening file "sample.txt" in out(write) mode
file.open("sample.txt",ios::out);
cout<<"File created successfully."<<endl;
//write text into file
file<<"ABCD.";

8
//closing the file
file.close();
//again open file in read mode
file.open("sample.txt",ios::in);
//read untill end of file is not found.
char ch; //to read single character
cout<<"File content: ";

while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}
file.close(); //close file
return 0;
9
}
Formative Assessment – Submit with
Example

Constructor vs Destructor
Overloading vs Overriding
Unary vs binary operator
Method in file stream class
10

You might also like