#include #include #include // The arguments to main will be the number of arguments on the command // lines (the name of the program is considered an argument) and an // array of C-style character strings containing the arguments. // The first one, argv[0], wille always be the program's name. int main(int argc, char * argv[]) { // Check that exactly two things were typed on the command line: // the command and a filename. Print an error and exit if not. // The convention is to return a non-zero value when an error occurs. if (argc != 2) { std::cerr << "Usage: " << argv[0] << " " << std::endl; exit(1); } // Open the file typed on the command line as a stream. If this // fails (for example, because the file does not exist), print // an error and exit. std::ifstream input(argv[1]); if (!input) { std::cerr << "Can't open file: " << argv[1] << std::endl; exit(1); } // Read all the strings in the file and print them out. // The loop will terminate when we reach the end of the file. std::string s; while (input >> s) std::cout << "Read string: " << s << std::endl; }