/* Example echoLines.cpp shinnerl@ucla.edu 2/14/2001 ----------------------------------------------------- Echo a file's contents to the screen. Notes: 1) Most operating systems automatically insert a '\n' character at the end of the file. 2) Once the end of the file is detected, the stream state changes from good to bad, and the boolean value of the stream changes from true to false. 3) When used as a boolean expression, the value of the getline(istream&, string&) function evaluates to the boolean value of its stream argument *after* the attempt to read a line has been made. */ #include #include #include #include using namespace std; int main(){ // Get the file name. string filename, oneLine; cout << "Enter the name of the file: "; getline(cin,filename); cout << "The file name is " << filename << "." << endl; // Open the file and attach the ifstream to it. ifstream is; is.open(filename.c_str()); // ifstream::open() requires a C-style string. if (!is){ cerr << "File not found." << endl; exit(1); } // Print the file's contents to the screen. while (getline(is,oneLine)) cout << oneLine << endl; return 0; }