Streams are the fundamental mechanism for text-based I/O (input/output) in C++, whether it's printing messages and taking input from the user via the terminal, reading and writing to files, or a number of other applications.
Programs can also receive input via command-line arguments provided when it is initally run.
We'll cover these as well as a number of miscellaneous topics related to programs and the command-line environment from which they run, including exit codes, input/output redirection, and pipelining.
1: Standard Input and Output
1.1
The familiar Here's the details and several examples:
1.1
Consider this command run at the terminal. Assume
Which of the following are true? |
2: File Streams
Streams are also used for reading and writing files in C++. First, some basics: In the previous video, we saw that the program should |
3: Patterns for File Input in C++
File input can be fairly complex, but there are a few common patterns that tend to work well. These depend on a some specifics of the stream operators and interface in C++, so we'll introduce those first. Now, on to some common patterns (and anti-patterns!) for file input in C++… |
4: Stream Functions and Unit Testing with Stringstreams
This section addresses a few design considerations for functions that performs input/output. First, it's generally best to pass generic Additionally, how can you write automated unit tests for the function? (You can't just have someone sit there and type input via The video below covers both in more detail. Fall 2024Please disregard the use of the |
5: Command Line Arguments
One last place we might like to take in input - when the program is originally launched from the terminal. For example, in project 2, the image resizing program takes arguments that look something like this:
Let's take a look at how this works in C++: |
6: Exercise: Word Count
6.1
In this exercise, implement a program that counts the number of words in a set of files, which are specified by providing their filenames as command line arguments. If a particular file does not open successfully, the program should print "Skipping file: " with the associated filename and continue on to the next file.
|
File Name | Contents |
---|---|
greeting.txt |
hello world!
|
fav_class.txt |
EECS 280 is awesome :)
|
fav_colors.txt |
red blue green
|
If the program was compiled to wordcount.exe
and run as:
./wordcount.exe greeting.txt aaaaa.txt fav_class.txt
The output to cout
would be:
greeting.txt has 2 words.
Skipping file: aaaaa.txt
fav_class.txt has 5 words.
7 words in total.
(Note that fav_colors.txt
was not specified and therefore ignored.)
Implement the program by filling in the boxes below.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
// MODIFIES: The given input stream
// EFFECTS: Reads all input from the given input stream and returns the
// number of words in the input (with words separated from each
// other by whitespace).
int word_count( input) {
string word;
int count = 0;
while() {
++count;
}
return count;
}
int main(int argc, char *argv[]) {
int total = 0;
for(int i = ; ; ++i) {
string filename = ;
ifstream fin();
if () {
cout << "Skipping file: " << filename << endl;
continue;
}
int wc = ;
cout << filename << " has " << wc << " words." << endl;
total += wc;
fin.close(); // technically not needed since fin is going out of scope each iteration
}
cout << total << " words in total." << endl;
}