File Handling in C++

File In a computer system files are used to store the necessary information / data stored. There are mainly two types of files:

  1. Text file
  2. Binary file

Text file. It stores information/data in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character.

Binary file. It contains the data/information in the same format as it is held in memory. In binary files, no delimiters or EOF are used for a line for terminate the line

Classes for file stream operation

By using stream classes we can perform different operation such as read a file or write a file. There are some stream classes:

  • ofstream: This stream class is used to write on files
  • ifstream: This stream class is used to read from files
  • fstream:  This stream class is used to both read and write from/to files.

Modes  of  file

Mode

Description

ios::in

open file for reading only

ios::out

open file for writing only

ios::binary

Open file in a binary mode.

ios::ate

Set the initial position at the end of the file.
If the value of flag is not set to any value, then the by default initial position is the beginning of the file.

ios::app

All output operations are performed at the end of the file.

ios::trunc

If the file opened for output operations,then its previous content is deleted and replaced by the new one.

Opening a file:There are two ways for open a file:-

OPENING FILE USING CONSTRUCTOR

OPENING FILE USING open()

Example: Write a program to open a file using constructor & write a character in a file using write() function

Output

Note: In the above program, we are opening a file x.txt which is exists in an E drive with the help of ofstream class object (f).

After that we will enter character (x) in a console and and write the entered character in a file using wrire() function.   Then we will close a file by using close () function.

Use of read() and write() function 

“read () and write()” functions are used  to performs the reading and writing operation in a file in a binary format.

The format of read() and write() function are as follows:  

read( (char*)&p , sizeof(p) );  

write( (char*)&p , sizeof(p) );           

These function receives two argument. The first argument is the address of the variable P and the second argument is the size of variable P in byte.

Example: Write a program to open a file using open () function & write a character in a file using write () function.

Output

Note: In the above program, we are opening a file x.txt which is exists in an E drive with the help of ofstream class object (f).

After that we will enter character (x) in a console and write the entered character in a file using wrire() function.  

Then we will close a file by using close () function.       

Example: Write a program read a character in a file using read () function.

Output

Note: In the above program, we are opening a file x.txt which is exist in a E drive.

After that we will read a  character (a) from a file “x.txt” and write that character in a console.     

Example: Write a program write and read a character in a file using read () & write () function. Use ifstream and ofstream class.

Output

Note: In the above program, we are opening a file x.txt which is exists in a E drive with the help of ofstream class object (f).

After that we will enter character (s) in a console and write the entered character in a file using wrire() function.

Then we will close a file by using close () function.    

Again we will open a x.txt file with the help of ifstream class object (f1) and read a character (s) from a file and write that character in a console.     

 Example:  Write a program to write and read a character in a file using read () & write () function. Use fstream class.

Output

Example: Write a program to write a string in a file using write () function.

Output

Example:  Write a program to read a string in a file using  read() function.

Output

Note:  Assume in a file “x.txt”, ”this is my book” is written. Then read () function will read only first string (this) or first 50 character string.

Example: Write a program to write and read a integer data in a file using read() & write() function. use ifstream and ofstream class.

Output

Example: Write a program to write in a file using  ofstream object.

Output

Example: Write a program to write and read in a file using ofstream & ifstream object.

Output

Note: In the above program, statement (f1>>a) will read only string “aaaa” from a file.  If we will again read string using ifstream object f1 then for example

Output

Find End Of File (EOF)

We can find the end of file by using the eof() function.

The eof() function returns the non-zero value when end of file is detected, otherwise eof() function returns zero.

Example:  Write a program to write and read in a file & display the content of file. Use eof() function.

Output

Note: In the above program, statement               

Initially function eof() return zero and condition will be true.

Then we will read a data from a file and print it in a console.

When function eof() detect the end of file then it will return non-zero value and then condition will be false and while loops terminates. 

 FILE POINTERS AND THEIR MANIPULATION

In a C++ file pointer is used to point to the reading or writing locations within a stream. There are  following member functions of file pointer

FunctionDescription

seekg()

moves pointer to a specified location in write mode

seekp()

moves  pointer to a specified location in read mode

tellp()

gives current position of the pointer  for write

tellg()

gives current position of the pointer for read

Use of tellg()and tellp() functions

tellg() – gives current position of the pointer  for write

tellp() – gives current position of the pointer for read    

Example: Write a program to show the use of tellg() & tellp() function.  

Or

Write a program to show the present position of input and output pointer using tellg() & tellp().

Output

Use of seekg()and seekp() functions

Function

Description

seekg()

moves pointer to a specified location in write mode

seekp()

moves pointer to a specified location in read mode

seekg() and seekp() function have a  two arguments. Their  format is fellows:-  

  seekg( offset , pre_position );  

seekp( offset , pre_position ); 

first argument specifies the number of bites the file pointer is to be shifted from the pre_position of the pointer.        

The pre_position may have one of the following possible values:-     

pre_position

Description

ios :: beg

Beginning of the file

ios :: cur

Current position of the file pointer

ios :: end

End of the file

File Pointer With Its Arguments in a seekg() function

Seek Option

Working

seekg(0,ios :: beg );

Go to the beginning of the file.

seekg(0,ios :: cur );

Rest at the current position.

seekg(0,ios :: end );

Go to the end of the file.

seekg(n,ios :: beg );

Shift file pointer to n+1 byte in the file.

seekg(n,ios :: cur );

Go front by n byte from current position.

seekg(-n,ios :: cur );

Go back by n byte from the present position.

seekg(-n,ios :: end );

Go back by n byte from the end of position.

Example: Write a program to enter text and again second time re-enter text from beginning position and replace the first word and display the content after 10 byte of the file pointer from the beginning of the file. 

Or

Write a program to show the use of seekp() and seekg() function.

Output

Note: In the above program, first, we write statement “aaaa bbbb cccc” then we  will shift the output  file pointer by using seekp() function  (f.seekp(0, ios :: beg);)at the beginning of the file and again write statement ”xxxx” in place of “aaaa”. 

Now the new statement/new content of file is “xxxx bbbb cccc”.

During reading file first we move the input file pointer 10 byte in a forward direction by using seekg() function (f1.seekg(10, ios :: beg);)   then we will read file till end of file.

Example: Write a program using seekg() to achive the following:   

  •  To move the pointer by 15 position.
  • To go backward by 20 byte from the end.
  • To go byte 50 in the file.

First code is

Second code is

Third is

Example:  Write a program to copy the content of one file into another file.

Output

Use of put() and get() function

get()” is a member function of the class fstream. This function reads the single character from a file.

“put()” function is a member of fstream class. The put() function write a single character in the file.

Example: Write a program to write and read the characters in a file using put() & get() function.

Output

Example: Write a program to write and read five characters in a file using put() & get() function.

Output

Use of fail() function

“fail()” stream state member function is used to check whether a file has been opened for input or output successfully or not.

 It returns the non-zero value if an operation is unsuccessful(or file is not opened) and return zero when it is open.

Output

Note: In the above program, fail() return 0 when file is open and condition will be true after that we can perform operation and return non-zero  value  when file is not open and condition will be false.

Categories C++