Fun Times with File I/O in C++

You use files every day; don’t deny it!  From the perspective of a program that you may write, a file is a way to have persistent storage.  What I mean by this is that the data you put in the file persists beyond the execution of the program.  So, if while your program is running, you put some data of some sort in your file, then when you run your program again tomorrow, that data should still be there.  Pretty much every program you use on a daily basis uses files.  Cool, right? Yea!

A Tad Bit of Background

From your previous programming experience, you probably remember the word inheritance.  Without going into the details of object oriented programming and such things, remember that inheritance means that two different units of code have something in common up the inheritance hierarchy.  This idea is important with file i/o in C++ because both console input/output and file input/output inherit from the same i/o infrastructure.

So, great, but I’m sure you may be asking why you should care about this.  Why?  Because it means that interacting with files will be almost exactly the same as interacting with cin and cout.  Right now, the only substantive difference for you is that cin and cout are automatically declared for you by including the iostream library.  With file i/o, you’ll have to declare the file object, “connect” it to a file, and then interact with the file.

The Details

The overland text doesn’t cover file i/o until later in the book and it mixes it up with other stuff that we don’t need right now (character file i/o is mixed with binary file i/o).  However, for completeness, you can find this stuff in section 13.6 of the Overland book.  However, if you go there and see scary things, close the book fast.  I’m mostly kidding, but don’t worry about understanding everything that is there right now.  Here are some more appropriate resources for the stage we’re at right now.

  • C++ Files and Streams by Tutorials Point
  • Basics of I/O Streams and File I/O by Prof. Steward Weiss of CUNY
  • I couldn’t find any good YouTube videos that I liked.  So, I’m going to make you listen to me more.  Here are 2 videos I recorded a couple semesters ago on File I/O (note, I’m compiling stuff in these videos from the command line, but the c++ code is the same):

Some Potential Pitfalls

At times, reading and writing to files can be frustrating for a few different reasons.  I’ll try and give you some insights here and will probably go over them “in-person” as well.

  • Trying to open a file, but the program can’t find it.  When a program attempts to open a particular file, it has to know what directory (aka folder) to look for the file.  If no file path is provided when opening the file in the program, it will look in the current “working directory”.  Now, if you’re running code from the command line, it will be the directory where the executable lives.  However, things get a little more complicated when it comes to using an IDE (integrated development environment) like Qt Creator because the IDE can define what the “working directory” is.  We’ll go over this in one of our live sessions.  But if you want to find out where to put a file for reading, the easiest way (not necessarily the best way though) is to open a file in a program for output, write some content to it, close the file, end the program, and then find the file through Finder on a Mac or the File Explorer on a PC.  You’d put your file to read in that same directory.  This is confusing, I know.
  • Not closing your file after use. This can be a big problem particularly when writing to a file because c++ uses buffered i/o.  All this means is that the content you intend to go to the file doesn’t necessarily end up there immediately after you execute the output command in c++.  It hangs out in a temporary location in ram (the buffer) until the buffer is full (then it writes it to disk) or until you explicitly tell it to flush the buffer.  There is a flush() function, but it is automatically executed when you close a file.  So, closing your files is like picking up your toys when you were done playing as a kid – not doing so will get you in trouble.

Speak Your Mind

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.