// This program shows how C-based file I/O works. It will open a // file, read in the first two strings, and print them to the screen. // included so we can use cout #include <iostream> #include <fstream> #include <stdlib.h> using namespace std; // we want to use parameters int main (int argc, char **argv) { // verify the correct number of parameters if ( argc != 2 ) { cout << "Must supply the input file name as the one and only parameter" << endl; exit(1); } // attempt to open the supplied file ifstream file(argv[1], ifstream::binary); // report any problems opening the file and then exit if ( !file.is_open() ) { cout << "Unable to open file '" << argv[1] << "'." << endl; exit(2); } // read in two strings string s1, s2; file >> s1; file >> s2; // output those strings cout << s1 << endl; cout << s2 << endl; // string comparison done with ==, but not shown here // close the file before exiting file.close(); }