// This program illustrates the use of eof controlled while loop. It shows 2 ways you // can read information from a file with reading stopping when the end of the file is // encountered. Formally the first while loop will stop when eof is reached or an // incorrect data type is encountered. #include #include using namespace std; int main() { char ch, filename[40]; ifstream fin; cout << "Please enter your input file name" << endl; cin >> filename; fin.open(filename); if(!fin) // didn't open file { cout << " Unable to open input file" << endl; return 1; } while (fin >> ch) // as long as there is a character to read from the file cout << " the character read from the file: " << ch << endl; //okay, now let's try this again using eof() cout<< endl << endl << " Now using eof test " << endl; fin.close(); fin.clear(); fin.open(filename); fin >> ch; while (!fin.eof()) { cout << " ch is: " << ch << endl; fin >> ch; } fin.close(); return 0; }