CSCI 1200 Data Structures
Fall 2011
Home
  Contact Information

Announcements
  LMS

Syllabus
  Learning Outcomes
  Prerequistites
  Grading Criteria

References
  Optional Textbooks
  Web Resources
  C++ Development
  Memory Debugging
  Misc. Programming Info

Getting Help
  Tutoring
  Advice from TAs

Calendar
  Lecture notes
  Lab materials
  Homework
  Test reviews

Schedule
  Office Hours
  Lab Times

Academic Integrity

Homework
  Due Date and Time
  Late Day Policy
  Compilers
  Electronic Submission

Misc. C++ Programming Information

Command Line Arguments

In order for your program to receive arguments from the command line, you will make use of the optional arguments to the main function. Here is the prototype:
  int main(int argc, char* argv[])
The parameter argc contains the number of strings on the command line (including the executable name). And the array argv stores those c-style strings. You can access the executable name with argv[0] and the arguments with argv[1], argv[2], etc.

Converting a C/C++ String to an Integer or Floating Point Value

First, be sure to #include <cstdlib>. Then, to convert a C++-style std::string variable to an int:
std::string my_stl_string = "17";
int x = atoi(my_stl_string.c_str());
Similarly, to convert C-style char array to a float:
char* my_char_string = "3.14";
float y = atof(my_char_string);

Reading From & Writing To Files

std::cin & std::cout are used to read data from and write data to the "console". Often we would rather read data from a file and/or write the output to a file. We can do this using the fstream library:
  #include <fstream>
Here's an example fragment of code that attempts to open an input file stream for a file name specified on the command line:
  std::ifstream in_str(argv[1]);
It is good coding practice to verify that the input stream was successfully opened:
  if (!in_str) {
    std::cerr << "Can't open " << argv[1] << " to read.\n";
    exit(1);
  }
Likewise here's how to open a stream for output:
  std::ofstream out_str(argv[2]);
  if (!out_str) {
    std::cerr << "Can't open " << argv[2] << " to write.\n";
    exit(1);
  }
Once the streams are created, you can use in_str & out_str just like you use std::cin & std::cout. In general, for this course, we encourage you to use the stream operator>> for all input parsing, and not use getline, eof, getc etc. We will not deduct points for using the other methods of parsing, but we have designed the assignment input format specifications for easy parsing with >>.

Note that the following code has a bug. If the input file ends with one or more extra newlines, the inner loop will process the last successfully read element twice. It is important to check the return value of each >> expression to be sure the read was successful.

while (!in_str.eof()) {
  in_str >> my_variable;
  // do something with my_variable
}
A simpler and more robust way to write the same code is:
while (in_str >> my_variable) {
  // do something with my_variable
}

Comparing Two Text Files

To check the correctness of your program, you can compare your output text file to the sample output text file using the UNIX utility diff (available on Cygwin & MacOSX):

  diff my_output.txt sample_output.txt

Any lines in the two files that are not identical (including whitespace), will be printed to the console. WinDiff is another option for Windows users. Please see a TA or the instructor in office hours if you have a question about these programs.

Redirecting Input & Output

What if you have an interactive program that uses std::cin & std::cout to read from and write to the "console", but you'd like to take the input from a file and you'd rather not rewrite the program to use the input & output streams described above. For example if you're debugging & testing an interactive program you'd rather not repeatedly type in the same test cases. Asking the executable to read from a file instead of the console and/or write to a file instead of the console is called file redirection.

With Cygwin/Linux/FreeBSD/UNIX, at the command prompt simply type:

   program.exe < input.txt > output.txt
With Visual Studio:
  1. Create input.txt file that contains input which you would otherwise type during program's execution.
  2. Move the file into your project's directory (where the project files are).
  3. Go to Project->Properties, select 'Debugging' and in 'Command arguments' enter: < input.txt > output.txt
  4. The 'Working directory' is by default the directory of your project files, so you don't need to change it. Warning: There is a file re-direction bug in Visual Studio .NET. The program does not change to the "working directory" before command line I/O takes place (< input.txt > output.txt). One workaround is to specify the full path of these files on your computer, e.g., c:\Documents and Settings\username\My Documents\cs2\hw\hw4\input.txt. Except... you can't have spaces in your file name. So you should put the files in a place with no spaces, like c:\input.txt.
    < c:\input.txt > c:\output.txt
  5. When you run your program from within Visual Studio, the input from the file input.txt will be redirected to your executable and the output text will be written to output.txt.