GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — C++ eof() | End-of-File in C++

  • C++ Course Basics
  • C++ Tutorial
  • C++ Basic Syntax
  • C++ Identifiers
  • C++ Character Set
  • C++ Input/Output Operator
  • C++ Variables
  • C++ Data Types
  • C++ Formatting Output
  • C++ Operators
  • C++ Assignment Operator
  • C++ Type Conversion
  • C++ Program Control
  • C++ if and if-else
  • C++ switch
  • C++ loops
  • C++ break and continue
  • C++ Functions
  • C++ Functions
  • C++ Prototype and Definition
  • C++ Function Call
  • C++ Function Types
  • C++ Friend Function
  • C++ Function Overloading
  • C++ Arrays and Strings
  • C++ Arrays
  • C++ One-Dimensional Arrays
  • C++ Strings
  • C++ String Functions
  • C++ Structures
  • C++ Structures
  • C++ Nested Structure
  • C++ Structure Array
  • C++ Pass Structure to Function
  • C++ Pointers
  • C++ Pointers
  • C++ Memory Map
  • C++ Declare Initialize Pointers
  • C++ Pointers and Structures
  • C++ Object-Oriented
  • C++ Object-Oriented
  • C++ Classes and Objects
  • C++ Constructors and Destructors
  • C++ Objects as Function Arguments
  • C++ Pointers and Objects
  • C++ Data Structure
  • C++ Linked List
  • C++ Stack
  • C++ Queues
  • C++ File Handling
  • C++ File Handling
  • C++ Opening and Closing Files
  • C++ Steps to Process Files
  • C++ Sequential I/O Operations
  • C++ Detecting EOF
  • C++ File Pointers Random Access
  • C++ Binary Files Operations
  • C++ Error Handling
  • C++ Misc
  • C++ typedef
  • C++ #define
  • C++ Date and Time
  • C++ Examples
  • C++ Examples

C++ eof() | End-of-File in C++

This post was written and published in order to explain the "eof()" method in C++ using an example program. So, without further ado, let us begin with a brief description.

You can detect when the file has reached its end by using the member function eof() of the "ios" class, which has the prototype:

int eof();

It returns non-zero when the end of the file has been reached; otherwise, it returns zero. For example, consider the following code fragment:

ifstream fin;
fin.open("master.dat", ios::in | ios::binary);

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

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

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

ifstream fin;
fin.open("master.dat", ios::in | ios::binary);
while(fin)
{
   :
}

C++ EOF Example

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

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

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

public:
   void getdata()
   {
      cout << "Roll Number: ";
      cin >> rollno;
      cout << "Name: ";
      cin >> name;
      cout << "Course: ";
      cin >> branch;
      cout << "Mark: ";
      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 << ", roll number " << rollno << " received " << marks << "% marks and ";
      cout << grade << " grade." << "\n";
   }

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

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

   int rno;
   char found;
   ifstream fin("marks.dat", ios::in);
   if (!fin)
   {
      cout << "Error opening the file!\n";
      return 0;
   }

   ans = 'y';
   while (ans == 'y' || ans == 'Y')
   {
      found = 'n';
      cout << "Enter the roll number 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 << "\nThe entered roll number does not exist in the file.\n";
         return 0;
      }
      cout << "\nWant to search more? (y/n):";
      cin >> ans;
   }
   fin.close();

   return 0;
}

The following snapshot shows the initial output produced by the above program:

c++ detecting eof

Now enter the data; for example, type "32041103" as the roll number and press the ENTER key, then "William" as the name and press the ENTER key again, "IT" as the course and press the ENTER key again, and "89" as the mark and press the ENTER key to save these records of a student in the file. The sample output after using these inputs is shown in the following snapshot:

c++ end of file

Now you can type "y" or "y" to enter the details for another student. For example, see the following snapshot:

eof c++

Now let me type "n" and hit the ENTER key to stop storing the data in the file. Now let me type the roll number and hit the ENTER key to search the student details regarding the given roll number. See the following snapshot:

c++ eof example program

I simply highlighted the text on the output console to draw your attention to the action reflected in the preceding run, ignoring the previous outputs.

More Examples

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

  • Read File
  • Write to File
  • Read & Display File
  • Copy File
  • Merge two File
  • List Files in Directory
  • Delete File
  • Encrypt and Decrypt Files

C++ Quiz


« Previous Tutorial Next Tutorial »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.