Reading from a File and Looking for the EOF Condition - C++ File Stream

C++ examples for File Stream:stream

Description

Reading from a File and Looking for the EOF Condition

Demo Code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void ReadFile(string filename)
{
    ifstream infile(filename.c_str());// ww  w  .  ja v a2s .  c  om
    int num;

    cout << "File: " << filename << endl;
    bool done = false;
    while (!done)
    {
        infile >> num;
        if (infile.eof() == true)
        {
            done = true;
        }
        else
        {
            cout << num << endl;
        }
    }
    infile.close();
}

int main()
{
    ReadFile("../nums1.txt");
    ReadFile("../nums2.txt");
    return 0;
}

Related Tutorials