Module 4 - Input and OutputModule Overview
1.) C++ is not the C Language -- Course Introduction
2.) C++ Batteries Included - STL and GDB Debugging with the STL
3.) Functions 1: Making our code more modular | GDB and Functions
4.) Input and Output
5.) C++: Thinking about memory
6.) Memory Allocation and Layout
7.) Arrays, Splines, and SFML Library Introduction
8.) Building Data Structures -- Structs and Linked Lists
9.) Physical Structure of a C++ Project
10.) No Class
11.) Graphics Applications, Textures, Images, Sprites, and 2D Arrays
12.) Object-Oriented Programming 1
13.) Object-Oriented Programming 2
14.) Object-Oriented Programming 3 - Composition & Inheritance
15.) Binary Tree Data Structure
16.) C++ Templates - Code Generation and Generics
17.) Smart Pointers and Quad Trees
18.) Iterators and Algorithms
19.) Concurrency and Threading
20.) Final Project Discussion
21.) Review/Functors/Lambda's/
22.) Graphs
23.) Holiday no class
24.) LValues, RValues, and Move Semantics
25.) Advanced Topic 1 - Scripting with C++ using Pybind11 and Lua
26.) C++ Tooling and C++ GUIs
27.) Course Wrap Up
☰ Select another Module

Audio/Video Recording


Readings/Warmup (Do before class)

Test Your Knowledge (Optional)

[Test your knowledge on the readings] (This is not graded)

...Commented Code Samples (if any)

For now I am linking code samples here: Github Code Repository for the course

Additional Resources


(Graded) In-Class Activity link

  • In-Class Activity Link
    • This is graded, and only your first response is graded
    • This is an evaluation of what was learned in lecture.
    • Due one week from when the lecture takes place

Module Content

Module Overview

In this module we discuss input and output!

Module Outline

  • Lecture outline
    • Streams
    • Input Output functions
    • File Reading and Writing
    • Text Parsing
    • std::filesystem


(Optional Notes)

Input and Output (I/O)

What makes our computers useful machines is the ability to input information, and retrieve the output of a computers computation. A simple example of input and output would be to use a calculator to compute the result of some arithmetic computation and compute a result for us humans. So one could argue that input and output is quite fundamental to what computers do!

Now there are many types of input and output that we might classify into different categories. For example:

  1. File input (writing data to a file), or file output (reading data from a file)
  2. Networks (Downloaidng information or uploading information)
  3. I/O with devices (peripheral controllers, like a game controllers button presses as input)

I think the most common form that we think of is with text processing. For example, we have looked at how to write out text to the terminal.

// SimpleOutput.cpp
#include <iostream>

int main(){

    std::cout << "Hello my programming friends!" << std::endl;

    return 0;

Note: With C++ 23 we have functionality for std::print. I think going forward using std::print will be a wonderful thing, but that does not dismiss the useful news of stream objects! See more:

// SimpleOutput_cpp23.cpp
import std;

int main(){

    std::print("Hello my programming friends!\n");

    return 0;

For now, I want to focus on text input and output.

stdin, stdout, and stderr

Behind the scenes it's important to understand what is going on when we process some data.

  • Talk about file descriptors
  • TODO: Insert diagram on File descriptors in a process

A note on debugging using std::cout

Often times when starting to program, I'll observe students writing little debug messages like std::cout << "I am here" << std::endl; in thier code. And while that is fine, we really should instead perefer std::cerr. The reason for this, is that std::cerr will make sure to write out the entirety of the message if the program crashes, which in some cases will not happen with std::cout.

Another note, often times you'll see me write std::endl at the end of my std::cout statements. The reason is that std::endl will always flush out whatever is in the std::cout object. For performance, constantly flushing the buffer can be bad (thus '\n' is preferred to end a line), though for the examples I present, the performance loss for constantly flushing the buffer is negligible.

C++ Streams

In C++, one of the mechanisms for handling input and output is a stream object. We have at least two that we want to become familiar with: std::cout and std::cin







Strings and std::string

  • Discussion on std::string
  • Discussion on internal representation of string
  • What is ASCII

File I/O Streams and std::filesystem

TODO, talk about fstream, ostream, istream, and then std::filesystem (C++17)

File Input


File Output


Example Text Writing Application

Please do not redistribute or host any materials without e-mailing me first. I generally am happy to share the latest .pdf or slide presentation with those who ask. Thank you for your time!