////////////////////////////////////////////////// // // fileInput.cpp // // Started by Jeff Ondich on 10/7/99 // Last modified 10/11/99 // // This program illustrates the use of the // ifstream type to take input from a file. // ////////////////////////////////////////////////// #include #include int main( void ) { // Get the file name. string fileName; cout << "Which file? "; cin >> fileName; // Open the file and make sure it's open. ifstream theFile( fileName.c_str() ); if( !(theFile.is_open()) ) { cout << "Cannot open " << fileName << ". Bye." << endl; exit(1); } // For each word in the file, print // the word and its length. string word; while( theFile >> word ) { cout << word << ": " << word.length() << endl; } // Now that we're done with the file, close it. theFile.close(); return( 0 ); }