AppDividend
Latest Code Tutorials

C++ File Handling Example | File Handling In C++ Tutorial

0

C++ File Handling Example | File Handling In C++ Tutorial is today’s topic. Most computer programs work with files because it helps into stores the information permanently. The information and data stored under a special name on a storage device are called a file. Stream refers to a sequence of bytes. A file itself is the bunch of bytes stored on some storage devices like tape, magnetic disk, etc. The binary file is a file that contains information in the same format as it is stored in memory. In binary files, are used for a line and no translations occur here.

C++ File Handling

In C++ language, files are mainly dealt by using the three classes fstream, ifstream, ofstream available in fstream header file.

  1. ofstream: Stream class to write on the files
  2. ifstream: Stream class to read from the files
  3. fstream: This Stream class to both read and write from the /to files.

#Operation performed on the file

  1. Creation of the new file
  2. Opening an existing file
  3. Reading from the file
  4. Writing to the file
  5. Deleting the file

#Classes for file stream operation

#ofstream

Stream class to write on files.

Example of using ofstream in program code.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char data[100];
	ifstream ifile;

	//create a text file before executing.
	ifile.open("text.txt");
	while (!ifile.eof())
	{
		ifile.getline(data, 100);
		cout << data << endl;
	}
	ifile.close();
	return 0;
}

See the following output.

 

C++ File Handling Example

#ifstream

Stream class to read from files.

Example of using ifstream in program code:-

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char data[100];
	ifstream ifile;

	//create a text file before executing.
	ifile.open("text.txt");
	while (!ifile.eof())
	{
		ifile.getline(data, 100);
		cout << data << endl;
	}
	ifile.close();
	return 0;
}

See the following output.

 

ifstream

#fstream

It can both read and write from/to files.

#filebuf

Sets the file buffers to read and write. It contains close() and open() member function in it.

#Opening and closing a file

Let’s Open File by using the constructor.

#Open File by using constructor

See the following code example.

ifstream (constr char* filename, ios_base::openmode mode = ios_base::in);
ifstream fin(filename, openmode) by default openmode = ios::in
ifstream fin(“filename”);

#Open File by using the open() method

See the following code example.

Calling of default constructor
ifstream filin;               //create an input stream
filin.open("filename", openmode);         //associate filin stream with file
       .
       .
filin.close();                              //terminate association with file master

#Different types of modes

in*

Stands for input and use to open a file for reading. Support input operations.

out

Stands for output and use to open a file for writing point of view. Supports output operations*

binary

Stands for binary and operations are performed in binary mode rather than in text format and anything else.

ate

Stands for at the end and in this output, operations start at the end of the file.

app

Stands for append and in this all the output operations happen at the end of the file, appending to the existing files.

trunc

Stands for truncate and with this, any content before the opening of the file is discarded.

All these flags can be combined with the help of bitwise operator OR (|). 

For instance, if we want to open a file example.scs in the binary mode to add data which we could do it by the following call to member function open(): 

fstream file;
file.open ("example.scs", ios::out | ios::app | ios::binary);  

#Default opening modes

#ifstream

ios::in

#ofstream

ios::out

#fstream

ios::in | ios::out

#Closing File

See the following code.

stream_object.close();
fout.close();
fin.close();

#INPUT AND OUTPUT OPERATION

#put() and get() function

The function put() is to write a single character to the associated stream.

The function get() reads a single character from the associated stream.

Example:

file.get(s);
file.put(s);

#write() and read() function

The write() and read() functions write and read blocks of binary data(file).

See the following code.

file.read((char *)&a, sizeof(a));
file.write((char *)&a, sizeof(a));

#STEPS TO PROCESS A FILE IN YOUR PROGRAM

  1. First pin down the type of link required.
  2. Declare the stream for the desired type of link.
  3. Attach the desired file to the stream.
  4. Now process as required.
  5. Close the file link with the stream.

#File Pointers

All the I/O streams objects have at least one internal stream pointer.

Ifstream is just like istream, which has a pointer which is called as the get pointer to read in the next input by pointing that element.

Ofstream is just like ostream, has a pointer which is called as the put pointer located the points where the next element has to be written.

At last fstream which inherits both, the get and put pointers, from iostream.

The other prototype for these functions is following.

seekg(offset, refposition );
seekp(offset, refposition );

The refposition takes one of the following three constants defined in the ios class.

ios::beg

ios::cur

ios::end

  Start of the file

  The current position of the pointer

  End of the file

Example:

file.seekg(-5, ios::beg)

Now, see the following table.

No. Function Description
1 fopen() It opens a new or existing file
2 fprintf() Helps to write data into the file
3 fscanf() It reads data from the file
4 fputc() It writes a character into the file
5 fgetc() It reads a character from a file
6 fclose() It closes the file
7 fseek() It sets the file pointer to the given position
8 fputw() It writes an integer to file
9 fgetw() It reads an integer from a file
10 ftell() It returns the current position
11 rewind() It sets the file pointer to the beginning of the file

 

Finally, C++ File Handling Example | File Handling In C++ Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.