Lesson 13: File Input / Output

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[spoiler title=”Lesson Source Code”]


Homework: Homework available here: http://beginnerscpp.com/forums/index.php/topic,73.0.html

File Input / Output
File input / output has been one of the most confusing topics for students. By the time they make their way to me they have tried slogging through assignments for hours. A few days ago I had a student approach me that spoke of working for 7 hours without being any closer to solving his solution. His code was a mess, and he was unable to explain how he had gotten to that point. So let me tell you before I even begin showing you code. There are differences between handling files and handling console input (via cin / cout) but for the most part, the two are VERY similar.

As you can see in the above code, all we need to do to read a line or to read in a single word is EXTREMELY similar in syntax to the usage of cin / cout that you should be familiar with by now. To read an entire file in this manner, we just expand the single statements into a while statement: