Codiqo

Learn C++ · lesson 48 of 61 · about 10 minutes

Capstone: Errors and Testing

Save and load the armoury with file streams, handle missing files, and check the logic with assert.

What you'll learn

  • Write and read files with fstream
  • Handle a missing file
  • Test behaviour with assert

A save the hero can trust

The armoury should survive the end of the game, so it's saved to armoury.txt, one item per line with | between the name, kind, power and whether it's equipped. loadArmoury() reads the file back with std::getline, building each item again, and gives back an empty armoury if the file doesn't exist. Then main proves it works: assert checks that a missing file loads as empty, that the reloaded armoury is the same size, and that every item has the same name and the same equipped state as before.

Files with streams

<fstream> gives files the same << and >> you use with cout and cin:

  • std::ofstream out("books.txt"); opens a file for writing
  • std::ifstream in("books.txt"); opens one for reading; if (!in) means it failed
  • std::getline(in, line, '|') reads up to a separator

The file closes automatically when the stream goes out of scope.

Save, load and test

#include <cassert>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

struct Book {
    std::string title;
    std::string author;
    int year;
};

void save(const std::string& path, const std::vector<Book>& books) {
    std::ofstream out(path);
    for (const auto& b : books) out << b.title << '|' << b.author << '|' << b.year << '\n';
}

std::vector<Book> load(const std::string& path) {
    std::vector<Book> books;
    std::ifstream in(path);
    if (!in) return books;
    Book b;
    std::string year;
    while (std::getline(in, b.title, '|') && std::getline(in, b.author, '|') && std::getline(in, year)) {
        b.year = std::stoi(year);
        books.push_back(b);
    }
    return books;
}

int main() {
    assert(load("missing.txt").empty());

    save("books.txt", {{"Dune", "Frank Herbert", 1965}, {"Emma", "Jane Austen", 1815}});
    auto loaded = load("books.txt");
    assert(loaded.size() == 2);
    assert(loaded[1].author == "Jane Austen");
    assert(loaded[0].year == 1965);
    std::cout << "All tests passed: " << loaded[0].title << ", " << loaded[1].title << "\n";
}

Output

All tests passed: Dune, Emma

Tip

assert(condition) stops the program with a message if the condition is false. It's the quickest way to check your logic as you build.

Saving true and false

Writing a bool to a stream gives 1 or 0, so the equipped flag is saved as a single digit. Reading it back, equipped == "1" turns it into a bool again. The same text goes out and comes back in, which is what makes the round trip easy to test.

A flag through a file

#include <fstream>
#include <iostream>
#include <string>

int main() {
    {
        std::ofstream out("flag.txt");
        out << "helmet|" << true << "\n";
    }
    std::ifstream in("flag.txt");
    std::string name, flag;
    std::getline(in, name, '|');
    std::getline(in, flag);
    std::cout << name << " saved as " << flag << ", equipped: " << (flag == "1") << "\n";
}

Output

helmet saved as 1, equipped: 1

Now try it yourself

This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.