In this video, we'll go over some examples of reading text files.
So I'm in the section 19 workspace, and I'm in the ReadFile_1 project.
And right now I've got nothing in here. The only thing I've done is I've included iostream,
and I've included fstream. We obviously need to include fstream to work with files.
That's all I've got.
Here is my project. You can see it's open and the file that I'm going to read is not there yet.
I wanted to show you how to create it.
First thing we want to do is let's write the code to open the file then it
will obviously fail because the file is not there.
And then we'll put the file in there, and we'll read it. Okay. So
first thing we need to do is we need to create an input file stream,
and we'll just call it infile, just like we've done in the slides.
And I'll initialize it on the next line,
I'll open it on the next line that way you can see that kind of syntax. So we'll say infile
.open. And here we give it a file name.
Now this is the part that's pretty operating system-specific. It's IDE-specific.
If you're in windows, you might be tempted to do this kind of thing
c:\
and then some directory and then some other directory maybe
followed by some
test.text or something like that, right.
That's the way that windows
file names look. The problem with this is
you remember that character literal that starts with a slash,
that won't work because that right there becomes a character.
So we want to do if you do it this way,
you want to use two slashes. Don't forget that's really, really important.
You would want to use two slashes everywhere if you're on windows.
Now if you're on mac or UNIX or some other operating system,
then you really don't do that. You kind of just you know slash
forward slash forward slash directory names to the file.
Now the good thing about this is even if you're on windows, you can still use this slash.
And behind the scenes, it'll be mapped to the proper one. So that's what I would recommend you do.
I would not recommend that you do anything like c colon or d colon
because that really, really ties your code to your computer.
So, for example, if you're trying to open a file in
c colon slash Frank slash test.
And then someone else tries to run this program,
unless they've got a Frank directory, it's not going to work. That file won't be there.
So the best thing to do is just to do something like that.
That way it'll look in the current directory, no matter what computer system you're on.
Now this is where the IDE issues come in, and I'll talk about that a little bit. For now,
let me just leave that, just like that I want to open a file called test.txt.
It doesn't exist yet here, as you can see, but we'll worry about that a little bit.
So we're trying to open that here.
The next thing we need to do is we need to see if it was open. So let's say if
not infile.
And remember, here we could say -- we could have said if not infile.is open,
like that, either one will work.
This is the style I like.
And that's the one I've been using a long time, and I'll keep using it. So if not infile.
That means we could not open this. Okay. So in this case, we're just going to say std cerr,
we're going to display an error message that says
opening file, let's say, just something real simple like that.
What do we do here, I want to get out of here. I've got nothing else to do.
I need this file to run my program. So I've got a couple of options.
I can say return anything other than 0. 0 means the program completed
successfully. I could return a 1. I could return a negative 1.
It really depends on what number you're looking for.
That means I couldn't find a file.
So we could -- I'm just going to say return 1.
Sometimes you'll see code that looks like this. It says exit 1
or more properly std exit.
And in this case, that calls a function named exit,
that closes up certain things and
destroys static variables and things.
But right now, I'm just going to simply return 1 if
I having some sort of problem opening the file. Now if I get beyond this piece right here,
I've got the file, it's good to go. So I'm just going to say std cout
file is good to go, I'm ready to read from it.
That's it. That's the program. This will fail when I run it. So let's try it.
I'm going to build and run.
And it says problem opening file, how come, because the file's not there.
Okay. So let's create the file. What I'll do is I'll come in here into my project.
And I'm going to click on the source directory.
This is what I would recommend that you do. Whatever files you create,
create them in the same directory where your .cpp files are.
Okay. So in this case, I'm going to put it right where my main cpp file is.
I'm going to right click on that source folder,
and I'm going to select add new file. You could also do it from up here, add new file.
And then I'm going to select any file and just give it a name.
And I'm going to give it this name, right here, test.txt.
So I'm going to say test.txt.
I'm going to press enter.
And now you can see that test.txt file right here,
and I can type some data into there and I will in a moment.
So now I've got the file. So now let's run it.
Still a problem opening file, how come.
Well, this is where the IDEs are all different.
If I open this file up right, this project up right here, I'm going to right click on it,
and I'm going to say open the containing folder just so I can open it up and explore here and look at it.
Let me move it over from my other monitor.
You can see that here's my c++ source file right here, my cpp file
right there.
And here is that test.txt file I just created, right.
But the executable's not here.
The .exe file is not here. It's in debug.
It's right here. That's my exe file, the application.
So when it runs, it's looking for that txt file in here,
and it's not finding it. That's why with CodeLite
we have to do something like this,
dot dot slash. So in other words I'm in debug, I need to go one
level above, one directory above me.
Now if I try to run this,
file is good to go, it found it.
Usually, this is what you want to do. Most IDEs will work this way,
CodeLite does not.
I believe code blocks and visual studio will
all work if you just type in the file name just like that.
And then what you do is when you create your file,
you create it where your cpp file is, and it'll find it.
Depending on what IDE you're using, you're just going to have to play around with it until you get it
and it opens it and then you're good to go.
Okay. XCode. If you're using XCode,
we'll talk about that in the video at the end because XCode is a little a little bit different.
C line, it'll just a regular test.txt will also work.
Okay. So that's it.
So now we've got a file, we're able to open it. So that's perfect. That's the first step, right.
We can't open it, we can't read it. So I'm going to get rid of this.
File is good to go statement. I really don't want to say that again because I've got it.
Now what i want to do is I just want to read some information from that file.
So let's put something in there. I'm going to go into t test txt,
and it's just the text file. So I'm going to say hello.
That's it. I'm going to -- I just saved it just now and I'm going to go back to main cpp,
and I'll create these variables up here, just so they're all in one place.
So let's create std string, and let's just call it line,
just any variable name will do. And down here,
what I want to do is,
remember, when we had cin, we would say cin into line.
Well, it's the same thing except we're not reading from cin, we're reading from infile.
So what that'll do is it'll read one string,
right. It's going to remember the way the extraction operator works. It'll stop at a space
or an end of file, it'll stop as soon as it sees some white space.
Now we should have that string in line, and we could just simply
display it
right to the console. Okay. So let me go through this again before I run it.
I'm opening the file, I'm testing to be sure that it's open.
If it's not open, I'm displaying problem opening file and I'm out of here.
Otherwise, I'm reading from the file into line. Line is a string,
and then I'm displaying whatever i just read.
Remember, the file contains hello, so that's what I should see,
right. So let's give it a run.
There's hello. So that came from that text file.
Okay. So now what we'll do is, let's read a couple of other things in here.
So I'm going to have an int. We'll, call it num, and we'll have a double.
And let's just call it total, just like we did in the slides. So now
what we'll do is we'll modify that file here,
so that it's got a string, an int and a double. It's already got a string.
So we'll give it an int, and let's give it a double,
200.345.
So that's my file. It's got a string, an int and a double. Okay. So now we'll come down here.
And what we'll do is we can read the string, and we could do this in separate lines,
we could just do it all in one. I'm just going to do it all in one to save a little time.
So I'm going to read the string, then I'm going to read the integer,
and then I'm going to read the total.
And what I'm going to do here is I'm going to display the line. I'll just display them in three lines.
I'll copy that one. So here we're displaying the line,
here we're displaying the num.
And then finally, we'll be displaying the total down here.
Okay. So all we've done now is rather than just read hello, we're reading
hello the integer and the double.
And when I run this, I should get three pieces of information displaying.
There they are. Hello 100 and 200.345.
Pretty cool. And if we modify this file to look like this,
this will still work, right, because we're using that extraction operator,
and it's going to stop right here when it sees a white space,
and it's going to stop right here when it sees a white space.
So we're pretty flexible as far as the format of this file.
So if I run this again, you can see I still got those numbers right there.
Okay. So that takes care of that example.
And what I'll do next is we'll go to read file two, and we'll do exactly the same thing except we'll
create more data here, and we'll read these inside a loop.Okay. So I'll be right back to do that.
And actually, before we go to the next example,
I forgot to close the file. So I wanted to be sure that you know that we need to close this file.
So I'm just going to do infile.close. Okay. That's very very important.
I got carried away with showing you how to read, I forgot the simple things to close the file,
so make sure you close the file at the end.
Okay. So I've switched over to section 19 workspace ReadFile_2 project.
Okay. So what we want to do now is rather than just read those three data items,
we want to continuously read three data items.
And let me show you what I did to the test file.
Now we've got Frank 100 and a double;
Larry, an integer and a double; Moe, an integer and a double and so forth.
So what i want to do is rather than just read the one line, I want to read as many lines as they're there.
So we'll need to do this in some sort of loop, right. So we're going to use a while loop.
And let me go back to my main, and I'll show you what's happening here.
Everything else is exactly the same. I have included
iomanip here just because I want to display those guys out at the end in a little bit of a
formatted column, that way you can see it a little better.
So I'm just displaying line num and total a little bit formatted.
So here I've opened the file, and I've checked that it's open.
I'm assuming it's open right here. Otherwise, I can't get here because the return 1 would kick me out.
So what do I want to do. Well, I want to have some sort of while loop here.
I want to loop while not
infile.end
of file eof, and that's a function.
That will return true if it's end to file
or no not true if it's not the end of file. And what I want to do in there
is basically display this. But first, I want to read the input, right.
So again, I'm reading from infile.
And first, I'm reading the line
actually should be name, right. But we'll just keep the name line, the num,
which is the int and the total.
And that should do it. So again, while we're not at the end of the file,
we're reading three items, displaying them, looping back up,
reading another three items, displaying them and so forth. Then when we're done,
this time I remember to close the file. Okay. So let's run this.
And when it runs, that's the information you get. You get Frank
Larry Moe Curly, and you get the integers and the doubles here nicely formatted
because I'm using the setw manipulator.
So we can do that with end to file, but we can also do that by putting that
this statement right here right inside the while.
So what we can do is we can refactor this a little bit.
And what we'll do is we'll put this statement right in there, I'll just move it up here,
just like that.
All right. So now this is a little different.
You have to wrap your head around this a little bit. So there's my infile,
and I'm reading those three items.
If I hit enter file, if anything fails, if I've got illegal data anything,
this will fail. And the while loop will fail and I'm out of here.
If I'm able to read all those three items, remember, this is using formatted
input because I'm using those extraction operators.
So that's it. It'll read them and then I'll display them.
So if i run this, I should get exactly the same output as before,
and there it is.
Okay. Cool so now in the next video --
this one's getting a little bit long, so I'll cut it off here.
In the next video what we'll do is we'll read one of Shakespeare's poems
line by line and display it out to the console.