Guided Notes

Files & Exceptions

Reading and writing files with fstream, checking that files opened, throwing and catching exceptions, and standard exception types.

File streams

Slide 1 / 5

#include <fstream>: • std::ofstream — write to a file, • std::ifstream — read from a file, • std::fstream — both.

std::ofstream out("scores.txt"); out << "Ada " << 91 << "\n";

std::ifstream in("scores.txt"); std::string name; int score; while (in >> name >> score) { ... }

Files close automatically when the stream object goes out of scope (RAII).

← Inheritance & Polymorphism