Skip to main content

Reading and Writing Files

beginner11 min readLesson 45 of 204

ofstream to write, ifstream to read, getline for lines, and the open-fail check that saves hours.

Writing a file

#include <fstream>

int main() {
    std::ofstream out{"notes.txt"};                 // open for writing (creates/truncates)
    if (!out) {                                     // ALWAYS check the open
        std::cerr << "cannot open notes.txt\n";
        return 1;
    }
    out << "buy milk\n" << "learn C++\n";
}                                                    // destructor closes โ€” RAII

No close() call: the destructor closes and flushes, even on early returns. That is module 12's RAII paying rent.

Reading a file line by line

std::ifstream in{"notes.txt"};
if (!in) { /* same check */ }
std::string line;
while (std::getline(in, line)) {
    std::cout << line << '\n';
}                                                   // loop ends at EOF

The getline loop is THE file-reading pattern; the stream's boolean state ends the loop exactly at end-of-file.

Append mode and file modes

std::ofstream log{"app.log", std::ios::app};    // append, do not truncate

Other modes exist (std::ios::binary, in|out) but Beginner lives in text mode.

The checks that matter

  1. Open check after construction (if (!out)) โ€” missing files, permissions, wrong paths.
  2. Read check in the loop condition โ€” handled by getline itself.
  3. Rare deep failures (disk full mid-write) are real but Intermediate's exception-safety territory; here, check the open and the loop.

Now practice

File Practice: Lines and LogsLog-line filtering, a word-count over multi-line text, and the fs-exists gate.1 challenge ยท ยท ~25 min