

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The tasks for lab 2 of computer science ii in fall 2006. Students will learn how to handle command-line arguments and files in c++ by using examples and exercises. The lab covers opening and reading files, as well as working with strings.
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


This lab explores the use of command-line arguments, file input and output, and strings. It is an important lab because it introduces tools that will be used in your labs and homework assignments throughout the semester. Create a folder for this lab in your CSII labs directory and download these files:
http://www.cs.rpi.edu/academics/courses/fall06/cs2/labs/02_strings_args/simple_stats.cpp http://www.cs.rpi.edu/academics/courses/fall06/cs2/labs/02_strings_args/in_numbers.txt http://www.cs.rpi.edu/academics/courses/fall06/cs2/labs/02_strings_args/in_strings.txt
Once you have downloaded these files, you need not access the network any further. Turn off all network connections.
Start by reading the code and comments in simple stats.cpp. It contains an explanation of what is needed in a program to handle command-line arguments and to open and read files. Then, just as you did in the last lab, compile/build this program in your favorite C++ development environment.
If you are using g++, run the program from your shell / command prompt by typing:
simple_stats.exe in_numbers.txt
If you are using Visual Studio, there are two ways to run the program, try both:
simple_stats.exe in_numbers.txt
If you created the program with a different project name than simple stats you will need to provide a different executable name.
Complete this checkpoint by demonstrating to a TA that you have successfully been able to run the program as described above. (Visual Studio users must show both methods).
Write a program that reads all of the strings from an input infile and copies all strings that are of length at least four to an output file, putting one string on each line. Your program will now have 3 arguments, one of which is the name of the program and the other two are the names of the input file and the output file (as argv[1] and argv[2], respectively). You should open both the input file and the output file before you start to read from the stream attached to the input file.
Output files are opened using a very similar mechanism to input file opening. In particular,
std::ofstream out_str( argv[2] );
will open the output file and attach it to stream out str. You can then check whether or not it was successfully opened using
if ( out_str ) ...
Once you have opened it and checked it, you are ready to use out str in the same manner that you used std::cout.
Complete this checkpoint by showing the execution and output of your program on the data set in strings.txt.