// OpenFilesCL.cpp // // Illustrates use of command-line arguments. // #include #include #include #include using namespace std; void InvocationError(); void SplashScreen(); void main(int argc, char* argv[]) { // Check for correct number of command-line arguments: // if (argc != 2) { InvocationError(); return; } // Check for help request: // (Remember the program name counts as one.) if ( (argc == 2) && (strcmp(argv[1], "/?") == 0) ) { SplashScreen(); return; } char* fileName = argv[1]; ifstream iFile; iFile.open(fileName); if (iFile.fail()) { cout << "File: " << fileName << " not found in current directory.\n"; return; } string Line; getline(iFile, Line); int LineNumber = 0; while (iFile) { cout << setw(5) << LineNumber << " " << Line << endl; getline(iFile, Line); LineNumber++; } iFile.close(); } void SplashScreen() { string Splash = "Displays named file to screen with line numbers.\n\n"; cout << Splash; InvocationError(); } void InvocationError() { string Invocation = "Invocation: OpenFilesCL \n" "where\n" " must be the name of an existing file\n" " in the current directory\n\n"; cout << Invocation; }