CodeBlanks Logo Icon CodeBlanks

Summary - File I/O

Combine ifstream, ofstream and getline to write a notes app.

Summary - File I/O

In this group you learned file input and output. std::ifstream reads, std::ofstream writes, std::fstream does both.

You check opening with is_open() and read line by line with std::getline. >> extracts whitespace-separated pieces, while std::getline reads up to a newline. By combining these two tools you can process structured text files. Now you will consolidate what you learned with two tasks: first counting lines, then summing numbers.

Practice Count the lines in the file

The starting code writes three lines to data.txt. Open the file with std::ifstream, count the lines with a while (std::getline(in, line)) loop and print the count with std::cout.

Starter code

main.cpp
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ofstream out("data.txt");
    out << "Line 1\nLine 2\nLine 3";
    out.close();

    // Count lines with getline and print the count

    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ofstream out("data.txt");
    out << "Line 1\nLine 2\nLine 3";
    out.close();

    std::ifstream in("data.txt");
    std::string line;
    int c = 0;
    while (std::getline(in, line)) {
        c++;
    }
    std::cout << c;
    return 0;
}

Expected output

3

Practice Sum the numbers in the file

The starting code writes 10 20 30 to nums.txt. Open the file with std::ifstream, read numbers in a while (in >> n) loop, sum them and print the total with std::cout.

Starter code

main.cpp
#include <iostream>
#include <fstream>

int main() {
    std::ofstream out("nums.txt");
    out << "10 20 30";
    out.close();

    // Read numbers, sum them and print

    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <fstream>

int main() {
    std::ofstream out("nums.txt");
    out << "10 20 30";
    out.close();

    std::ifstream in("nums.txt");
    int n, t = 0;
    while (in >> n) {
        t += n;
    }
    std::cout << t;
    return 0;
}

Expected output

60

End of section

You completed file input and output. You can now read with std::ifstream, write with std::ofstream and do both with std::fstream.

You check safe opening with is_open() and read line by line with std::getline. These five concepts — reading, writing, combined streams, open checking and line reading — form the foundation of file handling in C++. The next groups cover string streams and move semantics.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which function reads a whole line?

main.cpp
std::ifstream in("data.txt");
  • in >> line
  • std::getline(in, line) Correct answer
  • in.is_open()
  • in.close()

Explanation: std::getline reads a whole line.

2 Complete the is_open check

Question
if (in.____()) { std::cout << "Open"; }
Answer
if (in.is_open()) { std::cout << "Open"; }

Explanation: is_open() checks whether the file is open.

All lessons in Intermediate