// // Example of using the C++ file system. #include #include int main() { cout << "Enter file name: "; std::string fileName; cin >> fileName; // Open the file. Note the use of the c_str function. // This is necessary in that the // constructor expects an old C style character string. ifstream input( fileName.c_str() ); // If the file could not be open, report an error and terminate. if ( ! input ) { cerr << "Could not open: " << fileName << endl; return 1; } // Display the contents of the file once. char buff[256]; int lineNum = 0; while( !input.eof() ) { input.getline( buff, 256 ); cout << ++lineNum << ": " << buff << endl; } // Rewinding to the beginning of the file. // Note the clear to reset the EOF flag. input.clear( ); input.seekg(0); // Move read pointer back to 0 // Display the contents of the file a second time. lineNum = 0; while( !input.eof() ) { input.getline( buff, 256 ); cout << ++lineNum << ": " << buff << endl; } cout << "test complete" << endl; return 0; }