File Handling in C++

File Handling in C++ :

Operations on File :

Basic operations performed on a file are :
  • Creating /Opening file
  • Read from file
  • Write/Append into file
  • Closing file

For file handling in C++, fstream.h and iostream.h must be included in the program.

There are three classes/types of objects in C++ for file handling :

  • ifstream : It stands for input file stream. It is used to read the contents from the file.
  • ofstream : It stands for output file streamIt is used to create and write the contents into the file.
  • fstream : It represents file stream. It is used to read the contents from and create file or write contents into the file.

Creating/Opening Files :

File must be opened or created before using for reading/writing into it.
open() is used to open the file.
Syntax of open function is :
void open( const char *filename, ios_base::openmode mode );
filename specifies the name of the file or the location and the mode defines the mode in which file is to be opened i.e. reading, writing, appending.
File can be opened into various mode:
  • ios::in : It opens the file in reading mode.
  • ios::out :  It opens the file in writing mode.
  • ios::app : It opens the file in writing mode but appends data to the end of the file only.
  • ios::ate : It opens the file and takes us to the end of the file but we can move the put/get pointer to anywhere in the file.
  • ios::trunc : It truncates the contents of the file if it already exists and then opens the file for reading/writing
Two or more  modes can be combined,using the bitwise OR operator.
For ifstream, default mode is ios::in
For ofstream, default mode is ios::out|ios::trunc
For fstream, default mode is ios::in|ios::out

Writing into files:

Objects of the ofstream or fstream class are used to write into the files.

Reading from the files :

Objects of the ifstream or fstream class are used to write into the files.

Closing Files :

close() is used on the objects of the ofstream, ifstream or fstream to close the file streams.
e.g. 

OUTPUT :

File Pointers :

Each file object is associated with two file file pointers : get and put pointers. The input pointer(get) is used for reading the contents of a given file location and the output pointer(put) is used for writing to a given file location.
These are the following functions to manipulating these file position pointer :
seekg() : Moves get pointer to the specified position
tellg() : Get the position of the get pointer
seekp() : Moves put pointer to the specified position
tellp() : Get the position of the put pointer
seek function takes two arguments : First argument is a long integer which can be used to move specified no. of bytes forward/ backward from current position of get or put pointer.
Second Argument is used to specify the seek direction.
It can be
  • ios::beg : positioning relative to the beginning of a stream
  • ios::cur : positioning relative to the current position of a stream
  • ios::end : positioning relative to the end of a stream
e.g. 
fs.seekg(m,ios::cur);//Positions get pointer m bytes forward from current position
fs.seekp(0,ios::end);//Positions put pointer to the end
fs.seekp(-m,ios::end);///Positions put pointer m bytes backward from the end
 

Other Functions :

get() and put():

get() : Read a single character
put() : Write a single character
read() and write() :
These function are also used to read and write data into files.
These take two argument, first, is the character constant and the second is the size of that character constant.
e.g.
ifstr.read((char *)arg1,sizeof(arg1));
ofstr.write((char *)arg1,sizeof(arg1));

Leave a Reply

Your email address will not be published. Required fields are marked *