The course is part of this learning path
This course covers file input/output (file I/O). Being able to read from and write to secondary storage is important because often we want to keep some data persistently. By the end of this course, you will have the skills necessary to do this.
Learning Objectives
- Understand the fundamentals of both file input and file output and how to use both in tandem
- Use stream manipulators to make the structure of the data more organized
- Use dynamic memory, pointers, and classes to make more complex and interesting applications
Intended Audience
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- College students and anyone studying C++
Prerequisites
To get the most out of this course, you should have a basic understanding of the fundamentals of C++.
In the previous lecture, we discussed how to write data to a file using file output. We also discussed some formatting techniques as well. In this lecture, we will be exploring the complementary technique, i.e. reading data from files. This is often just referred to as file input. You may or may not have thought about it before, but whenever you save a file and then open a file, whether it's a word processing document, image file, or a video game character, the program is almost always using some form of file I/O. Saving a document involves writing a file in a format that the program will understand in order to read the data in and interpret it later. We're going to use a simple text editor such as Notepad or Notepad++ on Windows, for creating our file and you can use the corresponding text editor on your systems such as the Linux platform or Mac OS. You just have to make sure it can save plain text files. So, I wouldn't recommend trying to use something like Microsoft Word or anything like that for this task. So, Let's create a project first. Let's call it FileInputFun. Create a new project, Empty Project, FileInputFun. So, we can create our main, just to get our skeleton ready. There we go, main, those are skeleton or boilerplate code. All right. Okay, there we go. Hopefully, you see this type of code in your nightmares now, because you have typed it so many times. Alright, so before we write the majority of our code though, let's create a nice text file to be used for input. We'll create a text file named input.txt and populate it with some simple integers. Note that I'm going to save the file in the Project directory, inside the Solution directory of my Visual Studio project FileInputFund. But then if you're using a different IDE, you might need to put the file in a different location. When you type your numbers out in the text file, make sure there are no extra new lines at the end of the file. Also, you can choose however many numbers you want to put in your file to be read. So, we're going to create, I conveniently have my repo up here and we have FileInputFun as a folder. Alright, and we are going to go into the project, and I'm going to right click new text document and we're going to call it input.txt. Open that, put a bunch of numbers, it doesn't really matter. Some numbers, they don't have to be two digits, I'm just doing that. And notice that if I click pas this, the insertion point, the blinky thing does not go to the end. You do not want it to look like this. Do you see how it's blinking right there. Don't let it do that. You want it to be up here and you want there to be no extra spaces at the end. Alright, so we need to save it 'Ctrl+S' or File Save. Now, let's write some code to read the data from input.txt. So, we can close that, for now. Minimize it. Alright. So, we need ifstream, infile, infile.open, input.txt, and the corresponding close method call. We're going to be taking in an input number and also summing some numbers, good. Now in this case, we want to keep accessing input until the user or not the user but until the file is done. So, input. or infile rather .eof means end of file. As long as it's not the end of the file, this loop will keep going, so that's a sentinel controlled repetition. Excellent. You probably remembered that. All right, so that's what we're doing, we're accumulating this. This is an accumulator, so we're starting at zero. And every time it reads in a number, an integer, it will be added to the sum, += is our little shortcut. And then we have, outside the loop, sum of numbers is sum endl, and of course, we have our infile.close and then return zero. So, let's run it. All right. So, sum of numbers is 318. You can double check it. I've double checked it. It looks good to me. That was pretty awesome, wasn't it? So, we've at least got an idea now of how to read some data from file. It'd be really cool if we could keep track of all the numbers that we read from file and then echo them or print them back to the user along with the sum, wouldn't it? I'm glad you agree because I have a challenge for you. Using the same project FileInputFun, I want you to store all the integers as they're read in, in an appropriate data structure, which is a container of some sort, then we'll be able to handle an arbitrary number of items read in. I'm not explicitly telling you what to use, but it's something we've used before in the course. When you're done reading all of these numbers in, I'd like you to print them out before the sum is written to the console as it is in the current program. So, pause the video, come back when you're done or if you'd just like to see how I do it. How did that work out for you? Were you able to complete the challenge? This one required you to use some of your problem solving skills to decide on an appropriate or best data structure for the job. Although there are multiple possibilities, I'd recommend using a vector since it has everything we want. It can store an arbitrary number of elements, and this is perfect for what we're doing. Let's write the code. So, we need include vector, alright? And then maybe after our little summing stuff here vector of int myInts. And as we read the number in, we're just going to push back the input number and then sum them. Now before the actual sum is printed out, I want to use a loop. We can use an enhanced for loop if we'd like, range based for loop, cout the item, or you could use a regular old for loop and go up to the size of the vector. That's fine. And maybe put a little bit of extra space before the sum. All right. And of course, we need to run it to make sure it works. Start Without Debugging, and there we go. All of the numbers are printed out that we read in from file and says the sum of the numbers is 318. Excellent. Now you can see the numbers that were in the file read into memory stored in a vector to be used later. This is some of the awesome power of fileIO in action. We saved data to file for persistence and can load data back into memory when we need our programs to have direct access to it. In the next lecture, we will use what we've learned about both input and output with files to do both. We will solidify our knowledge up to this point and become more comfortable with the file operations. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.