Skip to main content

12.7) Exercise 18


  1. Start by reminding yourself of how command line arguments work in C++.
  2. Copy your Exercise 17 solution to a new file. Modify this program by first removing the code that populates the vector with data read from cin. In its place, add code that:
    • Checks that the argument count argc is equal to 2, terminating the program with the message "Error: filename required" if this is not the case
    • Opens a file for input, using argument argv[1] as the filename and terminating with the message "Error: cannot open file" if opening the file fails for any reason
    • Reads numbers from the file and appends them to the vector
  3. Create a small file containing a list of numbers. Run the program on this file and check that it produces the correct output.