codescracker


c++

C++ EOF



« Previous Tutorial Next Tutorial »


eof()

You can detect when the end of the file is reached by using the member function eof() which has prototype :

int eof();

It returns non-zero when the end of file has been reached, otherwise it returns zero.

For example, consider the following code fragment :

ifstream fin ;
fin.open("master", ios::in | ios::binary);
while(!find.eof())                        //as long as eof() is zero
{					  //that is, the file's end is not reached
	:                                 //process the file
}
if(fin.eof())                             //if non-zero
cout << "End of file reached ! \n" ;

The above code fragment processes a file as long as it EOF is not reached. It uses eof() function with the stream object to check for the file's end.

To detect end of file, without using EOF(), you may check whether the stream object has become NULL or not. For example,

ifstream fin;
fin.open("master", ios::in | ios::binary);
while(fin)               //as long as eof() is zero
{                   	 //that is, the file's end is not reached
	:                //process the file
}

C++ EOF Example

Here is an example program demonstrating how to detect end of file (EOF) in a C++ program:

/* C++ Detecting End-Of-File (EOF) - C++ EOF
 * This program demonstrates how to detect,
 * end of file (eof) in a C++ program */

#include<fstream.h>
#include<conio.h>
#include<stdlib.h>

class student
{
	int rollno;
	char name[20];
	char branch[3];
	float marks;
	char grade;

	public:
		void getdata()
		{
			cout<<"Rollno: ";
			cin>>rollno;
			cout<<"Name: ";
			cin>>name;
			cout<<"Branch: ";
			cin>>branch;
			cout<<"Marks: ";
			cin>>marks;

			if(marks>=75)
			{
				grade = 'A';
			}
			else if(marks>=60)
			{
				grade = 'B';
			}
			else if(marks>=50)
			{
				grade = 'C';
			}
			else if(marks>=40)
			{
				grade = 'D';
			}
			else
			{
				grade = 'F';
			}
		}

		void putdata()
		{
			cout<<name<<", rollno "<<rollno<<" has "<<marks<<"% marks and "<<grade<<" grade."<<"\n";
		}

		int getrno()
		{
			return rollno;
		}
}stud1;

void main()
{
	clrscr();

	ofstream fout("marks.dat", ios::out);
	char ans='y';
	while(ans=='y' || ans=='Y')
	{
		stud1.getdata();
		fout.write((char *)&stud1, sizeof(stud1));
		cout<<"Record added to the file\n";
		cout<<"\nWant to enter more ? (y/n)..";
		cin>>ans;
	}
	fout.close();

	clrscr();
	int rno;
	char found;
	ifstream fin("marks.dat", ios::in);

	if(!fin)
	{
		cout<<"Error in opening the file..!!\n";
		cout<<"Press any key to exit...\n";
		getch();
		exit(1);
	}

	ans = 'y';
	while(ans=='y' || ans=='Y')
	{
		found = 'n';
		cout<<"Enter rollno to be searched for: ";
		cin>>rno;

		while(!fin.eof())       // end-of-file used here
		{
			fin.read((char *)&stud1, sizeof(stud1));
			if(stud1.getrno() == rno)
			{
				stud1.putdata();
				found = 'y';
				break;
			}
		}
		if(found=='n')
		{
			cout<<"\nRollno not found in the file..!!\n";
			cout<<"Press any key to exit...\n";
			getch();
			exit(2);
		}
		cout<<"\nWant to search more ? (y/n)..";
		cin>>ans;
	}
	fin.close();
	cout<<"\nPress any key to exit...\n";
	getch();
}

Here are some sample output of the above C++ program:

c++ detecting eof

After entering the 3 records as shown in the above output, press n to stop entering record. After pressing n, just press enter and see the following sample run:

c++ end of file

As shown above, press any roll number (1-3). Here we have entered 1 to check the output. Now enter any roll number other than (1-3). Here we will enter 4. See the sample run here:

eof c++

More Examples

Here are some more examples listed, that uses files in C++ program, you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test