C++ ifstream eof()
end of file
#include <iostream> #include <fstream> #include <string> using namespace std; void ReadFile(string filename) { ifstream infile(filename.c_str());/*from w ww . j a va 2s. co m*/ 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; }
#include <fstream> #include <iostream> using namespace std; int main()//w ww . ja v a 2s .com { const int MAX = 80; //size of buffer char buffer[MAX]; //character buffer ifstream infile("TEST.TXT"); //create file for input while( !infile.eof() ) //until end-of-file { infile.getline(buffer, MAX); //read a line of text cout << buffer << endl; } return 0; }