CSCI 1200 Data Structures Fall 2011 |
|
Home Contact Information
Syllabus
References
Getting Help
Calendar
Schedule
Homework
|
Misc. C++ Programming InformationCommand Line ArgumentsIn 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 ValueFirst, 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 Filesstd::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 FilesTo 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 & OutputWhat 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.txtWith Visual Studio:
|