/////////////////////////////////////////////////////// // // files.cpp // // This program illustrates, very briefly, the // use of the ifstream class to read from a file, // and the ofstream class to write to a file. // // Revision history: // 2/16/98 (Jeff Ondich) First version. // 10/22/99 (JO) Reorganized for clarity. // 1/12/01 (JO) Rewrote to correspond better // to Savitch textbook. In particular, // used open() rather than constructor. // //////////////////////////////////////////////////////// #include #include // Prototypes. void InputFileExample( void ); void OutputFileExample( void ); int main( void ) { InputFileExample(); OutputFileExample(); return( 0 ); } //////////////////////////////////////////////////////// // Gets a file name from the user, reads the // words from the file and prints them, one per // line, to cout. If the file named by the user // does not exist, an error message is printed // and the program halts. //////////////////////////////////////////////////////// void InputFileExample( void ) { // Get the file name from the user. string fileName; cout << "From which file would you like to read? "; cin >> fileName; // Declare an ifstream object with fileName as // a constructor parameter to attempt to open // the named file. Check to make sure it // was opened successfully. ifstream myFile; myFile.open( fileName.c_str() ); if( !myFile.is_open() ) { cout << "Trouble opening " << fileName << endl; exit(1); } // (Technical note: The function open() expects its // parameter to be not a string, but a "char *". That is, // it expects what is usually called a "C string" to distinguish // it from a "C++ string". Fortunately, C++ has a way // to change C++ string temporarily into a C string. // You use the c_str() function, as shown above.) // Read words one at a time from the file, and // print them out, one per line. The end of // file is reached only after an attempt to read // past the last character. string word; while( myFile >> word ) { cout << word << endl; } // Clean up after yourself. myFile.close(); } //////////////////////////////////////////////////////// // Gets a file name from the user and writes a // foolish message into the file. If opening the // file fails, the program halts. //////////////////////////////////////////////////////// void OutputFileExample( void ) { // Get the file name from the user. string fileName; cout << "To which file would you like to write? "; cin >> fileName; // Declare and open an ofstream object // using the named file. Check to make sure it // was opened successfully. (See InputFileExample // for an explanation of "c_str()".) ofstream myFile; myFile.open( fileName.c_str() ); if( !myFile.is_open() ) { cout << "Trouble opening " << fileName << endl; exit(1); } // Write something to the file. myFile << "Nurse, I spy gypsies. Run!" << endl; myFile << "Ma, I say! Lee's as eely as I am!" << endl; myFile << "Satan, oscillate my metallic sonatas." << endl; myFile << "Go hang a salami. I'm a lasagna hog." << endl; myFile << "Flee to me, remote elf." << endl; // Clean up after yourself. myFile.close(); }