Variables vanish when the program ends. Files are how a program remembers between runs.
1 ofstream
Output file stream — creates or opens a file for writing, then << sends data into it.
2 ifstream
Input file stream — opens a file for reading, then >> pulls the data back out.
3 The usual operators
<< and >> work on files exactly like on cout and cin — you already know how to drive them.
4 close()
Flushes buffered data safely to disk and releases the file — or let the stream object clean itself up.
Persistence at last
ofstream1Output file stream — creates or opens a file for writing, then << sends data into it. ifstream2Input file stream — opens a file for reading, then >> pulls the data back out. The usual operators3<< and >> work on files exactly like on cout and cin — you already know how to drive them.
close()4Flushes buffered data safely to disk and releases the file — or let the stream object clean itself up. 4 building blocks make up Persistence at last
Files can be missing or locked — the stream tells you, if you ask it. Follow both branches.
process decision input / output start / end Write today, read tomorrow — the four stream moves that make data survive.
Press play — two items go to the file, then >> pulls them back to the console one pair at a time.
preprocessThe preprocessor pastes in fstream — ofstream and ifstream live here.
Memory
no variables yet — press play
Console
output appears as the program runs…
Click a term, then click its description to lock the pair.
6 conceptsTap a card to expand ?ofstreamdrops into place
?ifstreamdrops into place
?out << "hi"drops into place
?in >> worddrops into place
?close()drops into place
?<fstream>drops into place
A loop writes to a file — what reaches the console?
Your predicted output — one line per console line
Hint: The loop writes 10, 20 and 30 into log.txt on disk — only the final cout line ever reaches the console.
01ofstream writes, ifstream reads — fstream is the header for both.
02<< and >> work on files exactly like on the console.
03Check that the stream opened before trusting it with data.
04close() flushes data to disk; destructors do it for you at scope exit.
05while (in >> x) loops until the data runs out — the classic read pattern.