Source code for ./labs/lab11/fileio2.cpp (via source-highlight)
// 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.
#include <iostream>
#include <fstream>
#include <cstdlib>
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;
// close the file before exiting
file.close();
}