codescracker


c++

C++ File Streams



« Previous Tutorial Next Tutorial »


A stream is a name given to a flow of data at the lowest level. At the lowest level, data is just the binary data without any notion of data type. Different streams are used to represent the different kinds of data flow such as whether data is flowing into the memory or out of the memory.

Each stream is associated with a particular class, that contains the member functions and definitions for dealing with that particular kind of data flow. For instance, the ifstream class represents the input disk files.

The stream that supplies data to the program is known as the input stream. It reads the data from the file and hands it over to the program. The stream that receives data from the program is known as output stream. It writes the received data to the file.

The file I/O system of C++ contains a set of classes that define the file handling methods. These classes, designed to manage the disk files, are declared in the header file named fstream.h. Therefore, we must include this file in a program that works with files. The classes defined inside the header file, fstream.h, derive from the classes under the header file iostream.h, the header file that manages console I/O operations.

Therefore, if you include the header file fstream.h in your file handling programs, you need not to include iostream.h header file as classes of fstream.h inherit from iostream.h only.

The function of these classes have been summarized in the following table :

Class Functions
filebuf It sets the file buffers to read and write. It contains close() and open() member functions to it
fstreambase This is the base class for fstream, ifstream and ofstream classes. Therefore, it provides operations common to these file streams. It also contains the functions open() and close()
ifstream Being an input file stream class, it provides the input operations for file. It inherits the functions get(), getline(), read() and functions supporting random access ( seekg() and tellg() ) from istream class defined inside the header file iostream.h
ofstream Being an output file stream class, it provides the output operations. It inherits the functions put() and write() along with functions supporting random access ( seekp() and tellp() ) from ostream class defined inside the header file iostream.h
fstream It is an input-output file stream class. It provides support for the simultaneous input and output operations. It inherits all the functions from istream and ostream classes through the iostream class defined inside the header file iostream.h

C++ File Streams Example

Here is a simple example program related to C++ file stream. This programs only ask to enter the file name and then to enter a line to store the line in this file.

/* C++ File Streams */

#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
	char inform[80];
	char fname[20];
	char ch;
	clrscr();

	cout<<"Enter file name: ";
	cin.get(fname, 20);
	ofstream fout(fname, ios::out);

	if(!fout)
	{
		cout<<"Error in creating the file..!!\n";
		cout<<"Press any key to exit...\n";
		getch();
		exit(1);
	}
	cin.get(ch);

	cout<<"Enter a line to store in the file:\n";
	cin.get(inform, 80);
	fout<<inform<<"\n";

	cout<<"\nEntered informations successfully stored..!!\n";
	fout.close();
	cout<<"Press any key to exit...\n";
	getch();
}

Here is the sample run of the above C++ program:

c++ file streams

More Examples

Here are some C++ programs listed. These programs uses files in C++:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test