Files are cout and cin pointed somewhere that lasts. The only new thing is that the other end can be missing — and C++ will not raise its voice about it.
Everything your programs have done so far has vanished when they stopped. Files are how work survives — and there is almost nothing new to learn, because the arrows are the ones you already know.
cout and cin pointed somewhere else. The only genuinely new thing is that the other end can be missing — and C++ will not raise its voice about it.Open, use, close. The o and i at the front tell you which direction, and the arrows match — out to the file with <<, in from the file with >>.
Watch the disk panel. Then try reading a file that was never written.
# nothing yet
A missing file does not crash your program and does not print an error. ifstream simply opens unsuccessfully, every read does nothing at all, and your variables keep whatever they already had — often zero. Your program then reports a perfectly confident total of 0. Verified: is_open() returned 0 and nothing else happened.
So check, every time. It is two lines:
You almost never know how many lines a file has. So you do not count — you read until reading stops working, which is a while from level 7 with the read itself as the condition:
That reads as “while there is another line to get, get it”. For numbers rather than lines, while (in >> number) does the same job. Both stop cleanly at the end of the file, and both stop immediately if the file never opened — which is precisely why the check above matters: without it, an unopened file and an empty file look identical.
"scores.txt" with no path means the folder your program is running in, which is wherever your Terminal was when you typed ./program — not necessarily where the file is saved. “It cannot find my file” is almost always this, and ls from level 1 settles it in one command.
Decide a format and write five records on the paper. Names and scores, one per line? Comma separated? You choose — but do not say which.
Work out the format from the paper alone, then say exactly which C++ reads it: getline, or in >> name >> score?
The argument that follows is the real lesson: a name with a space in it breaks in >> name, and a comma-separated line breaks it too. Whoever wrote the file decided how hard it is to read, and they usually did not think about it. That is most of what working with real data is.
ofstream out, ifstream in, and the same arrows you already know.
A missing file is silent. Without the check, no data and empty data look the same.
while (getline(in, line)). Do not count lines you have not seen.
Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
You read a file that does not exist. What does your program do?
How do you read a file when you do not know how many lines it has?
Your program cannot find scores.txt, but you can see the file on your desktop. Why?
← back to the whole C++ track · stuck on anything? ask peter.