File Handling

Lesson 20 of 20

Programs that remember — ofstream writes, ifstream reads, and the full file round trip is animated for you.

The big picture

Persistence at last

Variables vanish when the program ends. Files are how a program remembers between runs.

ofstream

Output file stream — creates or opens a file for writing, then << sends data into it.

ifstream

Input file stream — opens a file for reading, then >> pulls the data back out.

The usual operators

<< and >> work on files exactly like on cout and cin — you already know how to drive them.

close()

Flushes buffered data safely to disk and releases the file — or let the stream object clean itself up.