Loading....
Coupon Accepted Successfully!

 

Finding End Of A File

While reading data from a file, it is necessary to find where the file ends, that is, the end of the file. The programmer cannot predict the end of the file. If in a program, while reading the file, the program does not detect the end of the file, the program drops in an infinite loop. To avoid this, it is necessary to provide correct instructions to the program that detects the end of the file. Thus, when the end of the file is detected, the process of reading data can be easily terminated. The eof() member function() is used for this purpose. The eof() stands for the end of the file. It is an instruction given to the program by the operating system that the end of the file is reached. It checks the ios::eofbit in the ios::state. The eof() function returns the non-zero value, when the end of the file is detected; otherwise, it is zero.
 

16.7 Write a program to read and display contents of file. Use eof() function.

#include<fstream.h>

#include<constream.h>

#include<iomanip.h>

main()

{

clrscr();

char c, f_name[10];

cout<<“\n Enter file name:”;

cin>>f_name;

ifstream in(f_name);

if (!in)

{

cerr<<“ Error in opening file”<<f_name<<endl;

return 1;

}

while (in.eof()==0)

{

in.get(c);

cout<<c;

}

return 0;

}

OUTPUT
Enter file name : text
Programming with ANSI and TurboC
 
Explanation: The above program is similar to the previous one. Here, the member function eof() is used in the if() statement. The program displays the contents of the file text. While specifying a file name for reading purpose, be sure that it exists.
 

16.8 Write a program to detect end of file using function eof(). Display the values returned by the eof() function.

#include<fstream.h>

#include<constream.h>

#include<iomanip.h>

main()

{

clrscr();

char c;

ifstream in(“text”);

while (in.eof()==0)

{

in.get(c);

cout<<in.eof();

}

return 0;

}

OUTPUT
00000000000000000000000000000000000000000000000001
 
Explanation: As explained earlier, the eof() returns one when the end of the file is found; otherwise, it is zero. Thus, until it returns zero, the program continues to read data from the file and when the end of the file is detected, the reading routine is terminated and one is displayed. In the above program, the while() loop checks the return value of the eof() function. When the eof() function is called, the following codes are executed:
 

ios::operator void *()

{

return fail() ? 0: this;

}

 
The above function converts an ifstream object into a void pointer. It also invokes the ios::fail() function, which is as follows:
 

int fail()

{

return & (failbit | badbit | hardfail):

}

 
The above function returns the non-zero file when the end of the file is not detected. The function returns the address of the object using this pointer. Zero is returned when the end of the file is reached. The while() or if() statement checks whether the value is zero or non-zero (address of object). Here, zero means false, and non-zero means true. The details of the above functions are discussed later in the same chapter.




Test Your Skills Now!
Take a Quiz now
Reviewer Name