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: Generative AI in EECS 280
1.1
First, a miscellaneous but important topic - the use of generative AI toos to write project code in EECS 280. The quick version: don't do it - you won't learn as much and your grades may reflect this. But, the long version is worth hearing. And we've been hard at work collecting some pretty fascinating data to back it up.
1.1
Which of the following appear to be plausibly true based on our data + analysis of genrative AI usage in EECS 280 projects? |
2: Standard Input and Output
2.1
The familiar Here's the details and several examples:
2.1
Consider this command run at the terminal. Assume
Which of the following are true? |
3: 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 |
4: 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++… |
5: 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 2025Please disregard the use of the |
6: 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++: |
7: Exercise: Word Count
7.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;
}