Okay. In this video what I want to do is read a poem from a text file
and just display it out to the console.
And we're going to use getline to do that.
So I'm in the section 19 workspace, in the readfile_3 project.
And what I've done here is I've got a poem already and I created
a file called poem.text, just like we did before.
And this is Shakespeare's sonnet 18 poem.
And you can see it's 14 lines. And what I want to do is read
each line and display it out to the console.
Now if we try to do this with the extraction operator
reading one of these words at a time.
It's much trickier because when do we hit the end of line,
we really don't want to deal with any of that.
The simplest way to do it is just to read one line at a time
and then display that line right out. So let me show you where the main is.
This is about as simple as it gets. I've got iostream,
fstream included.
You can see here I've created my infile. And I'm opening
poem.txt in my parents directory, which is exactly
where I want it to be in CodeLite.
If the file is not able to be opened I'm displaying an error, again, and I'm out of here.
And I'm closing the file at the end. So what I need to do here is simply process that file.
So how do we do this, it's really just two lines of code. It's really, really straightforward.
So what I'm going to do here is just say while, I'm going to use std getline.
And there's two versions of getline by the way.
There's a getline that works with character pointers or c-style strings.
And there's a version that works with c++ strings.
We're using c++ strings from now on in the course so that's what we're going to be using.
So we're using std getline
give it the name of the input stream, which in this case is infile.
And where do I want this read into, I want this read into line.
We haven't defined line yet. So let's define line. I'll do that right here.
I'll say std string line.
That's it. That's where the input's going into.
So what do we do.
Well, while we're getting a line, and it's successful, this will be true.
So what do we want to do with that line. We just want to display it, right, to the console.
So can't get much easier than this. Cout
line and I need a new line.
That's it. That's really that simple. So if I run this
there's the poem,
right. One line at a time till I hit the end of the line.
I'm reading that in. And I'm just displaying it right back out.
Okay. Pretty easy, right. Now you can see how easy it would be to write a file copy utility.
We haven't learned about writing files yet thinking you know where we're going, right.
We can just like -- we write to std cout we can write to a file.
So we can easily write a utility that will copy a file. And we're actually going to do that.
We're going to when we get to the output file section we'll read this file,
and maybe this one or Romeo Juliet or something a bigger one,
and we'll write it out with line numbers and so forth. That way we can modify it a little bit.
So that's it. This is using getline, reading one line at a time.
Now what I want to do next is I want to go into ReadFile_4 project.
And what we'll do is we'll do the same thing, but we'll use unformatted input.
So we'll do it one character at a time. So I'll set that up, and I'll be right back.
Okay. So I'm back in the ReadFile_4 project for the section 19 workspace.
And what we're going to do here is we're going to read sonnet 18 again,
except we're going to use unformatted
input. In this case, we're just going to use the get method
and what that'll do is it'll return one character at a time.
And we'll just display that one character out.
So the advantages of using unformatted input are
you don't have to worry about new lines, you don't have to worry about end of files, you don't have to worry about
fancy characters and different things that could maybe break the way that you're reading
because it's formatted,
and it's just going to process characters unformatted, it's pretty cool.
It gives you a lot of flexibility. And that's what we'll do now. So
this is where we're at again. We've got iostream and fstream, exactly where before.
We've created our infile object and opened the file,
and we've tested to be sure we've got an open file.
We're closing it down here. So right in here
is where we're going to do our processing of the file.
How do we do this. Well, pretty simple again. While
infile.get
sum character c.
Okay. We need to define c as a character, so we'll do that right here.
We'll say char c.
That's it. It's really pretty simple. So while this is true,
what do I do? Std cout
the character.
And then that's it. I mean down here, I guess we could print
a new line here just so we get a little bit of space at the end so we can see a little better.
But that's it. So while we're reading a character and it's true,
right, we haven't hit end of file, we haven't hit some bad bits, we haven't read some --
we haven't had any hardware issues, while all this is good,
we've got that c,we're just going to display it.
So if we run this,
there's the poem, exactly like it was before.
That's it. Now you're ready for the challenge,
and the challenge is going to be a fun one. There's a couple of challenges actually.
So I'll see you in the next video, and I'll explain the challenge to you.