codescracker


c++

C++ File Handling



« Previous Tutorial Next Tutorial »


Sometime, it is important to store the information entered by the user into the file for further use. After storing the information into the file, later you can retrieve those information from that file. File helps in storing the information permanently.

A file itself is a bunch of bytes stored on some storage devices like taps, or magnet disk etc. In C++, the file input/output operation are performed through a component header file fstream.h of C++ standard library.

In C++, a file

The fstream.h library predefines a set of operations for handling file related input and output operation. It defines certain classes that helps in performing the file input and output operations.

You will learn all about file handling in C++. C++ file handling divided into separate chapters such as

C++ File Handling Programs

Let's take some example to know, how file handling is useful in C++ to store the information entered by the user for later use. Here are some sample programs listed, demonstrating how to handle files in C++. You will learn all about handling the file in C++ in separate chapters as mentioned above. If you have previous knowledge on C++ file handling, then you will find better to read examples listed below. Otherwise, just see the examples, and follow next tutorial. After sometime, you will also find better and ease in handling files in C++.

/* C++ File Handling - Store information into the file in C++ */
		
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
	ofstream fout;
	char fname[20];
	char rec[80];
	clrscr();

	cout<<"Enter file name: ";
	cin>>fname;

	fout.open(fname, ios::out);
	cout<<"Enter a word: ";
	cin>>rec;
	fout<<rec;
	cout<<"Data inserted successfully..!!";

	fout.close();
	getch();
}

Here, this C++ program ask you to enter a filename to create/overwrite the file and then ask to enter a word to store this word in the file. Here is the sample run of the above C++ program:

c++ file handling

After running the above program, with entering the same information shown in the above output. You will find, a file named "myfile.txt" having "programmer" written on this file. Therefore, you are always free to retrieve this word from this file later.

Let's take another example to know how to retrieve this word.

/* C++ File Handling - Retrieve information from the file in C++ */
		
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
	ifstream fin;
	char fname[20];
	char rec[80];
	clrscr();

	cout<<"Enter file name: ";
	cin>>fname;

	cout<<"The file contains:\n";
	fin.open(fname, ios::in);
	fin.get(rec, 80);
	cout<<rec;

	fin.close();
	getch();
}

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

file handling in c++

As you can see from the above output, the word is retrieved from the file (named myfile.txt) that we have used earlier to store the word (named programmer). Let's take an another example. This is the modified and complete version of the above two programs.

/* C++ File Handling - This program demonstrates
 * how to handle file in C++. That is, how to
 * write some content to a file and retrieve
 * the content from the file to display it on
 * the screen in C++. */

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
	char rec[80], ch;
	ofstream fout("file.txt", ios::out);
	clrscr();

	cout<<"Enter 4 lines of information: \n";
	for(int i=0; i<4; i++)
	{
		cin.get(rec, 80);
		cin.get(ch);
		fout<<rec<<"\n";
	}
	fout.close();
	ifstream fin("file.txt", ios::in);
	fin.seekg(0);
	cout<<"\n";
	for(i=0; i<4; i++)
	{
		fin.get(rec, 80);
		fin.get(ch);
		cout<<rec<<"\n";
	}
	fin.close();
	getch();
}

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

c++ file handling programs

Here is the complete version of the above programs. This program is the complete version of the C++ file handling program.

/* C++ File Handling - This is the complete
 * version of the C++ file handling program.
 * This program creates a file (entered by user)
 * and store some content (entered by user).
 * Then display those content (if user want)
 * on the output screen in C++ */


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
	char rec[80], ch;
	char fname[20];
	int count=0, i;
	char ans='y';
	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 information to store..\n";
	while(ans=='y' || ans=='Y')
	{
		cin.get(rec, 80);
		fout<<rec<<"\n";
		cout<<"Want to enter more ? (y/n).. ";
		cin>>ans;
		count++;
		cin.get(ch);
	}
	cout<<"\nThe information successfully stored in the file..!!\n";
	fout.close();

	cin.get(ch);
	cout<<"Want to see ? (y/n)..";
	cin>>ans;

	if(ans=='y' || ans=='Y')
	{
		ifstream fin(fname, ios::in);
		if(!fin)
		{
			cout<<"Error in opening the file..!!\n";
			cout<<"Press any key to exit..\n";
			getch();
			exit(2);
		}
		fin.seekg(0);
		cout<<"\n";

		cout<<"The file contains:\n";
		for(i=0; i<count; i++)
		{
			fin.get(rec, 80);
			fin.get(ch);
			cout<<rec<<"\n";
		}
		fin.close();
	}

	getch();
}

Here are the two sample run of the above C++ program:

c++ file handling examples

file handling programs in c++

More Examples

Here are some more examples listed for file handling, that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test