Everybody, welcome back to another lecture.
So in this lecture, we are going to be talking about files, dreams, files, dreams are a way to read
and write data to files.
So to and from really so we can read data from files and write data to files.
So we're doing something similar that we've to what we've done so far with the console, right?
We've been able to read data from the console with CNN, and we've been able to write data to the console
with see out.
Now we're going to talk about how to use files.
So we're kind of getting more real.
And by that, I mean, reading from files is a lot more realistic than having a program that only takes
input from the console.
So we're about to learn how to not only take input right and read from files, but we're going to learn
how to write from them as well.
So that requires us to include something called f stream.
So it's similar to the Io stream, right, which is input output stream, but instead this is used for
files instead of reading writing to console.
So this time the stream is going to be flowing to and from a file.
So the f here stands for file straight right, f for file and then stream similar to input output stream.
The first thing we're going to talk about is reading from a file.
So this is if stream or you can think of it as input file stream.
So to do this, what we're going to need to do is create a variable of type extreme stream, which is
the input file stream, right?
So it's similar to like if we had some integer my aunt right, we say it would be like integers, the
data type and my end is the variable.
This is extreme as the data type and my stream as the variable name.
So the next thing that we need to do is connect the stream to the file that we want to read by using
a special function that can open that file.
You notice right here we have our variable right, which is the stream that we created, that we declared
my stream.
And then we use the dot open function, right?
So this is a member function as a dot.
So this is kind of similar to when we had a string variable and did dot length.
But now what we're doing is recalling the open function on the mice dream input, file stream, variable
rate, my stream dot open is going to enable us to open the file that's inside the parentheses here
as an argument, right?
And notice that this is a string as a double quotes.
So the open function requires and expects a string to be in here, right?
So we're going to put double quotes and that is going to be the file that we want to read data in from.
So this case and this example, I'm saying my file, that text.
Right?
That is what we are going to open to be able to read data in from, and we're connecting it to our stream
here.
So at this point in time, we are ready to read into a variable, just like we did with console input,
which was seen right.
So here I declare a variable called my aunt.
And here, similar to how you see in, I have this was what this is basically where CNN used to be right
when we were dealing with C and it was on this side of this.
Operator And then our variable is on this side and the data kind of flow was flowing from the console
and into our variable.
Same thing here.
The data is flowing from the input stream.
This is our input file stream, right?
It flows from there into our integer that we declared, so this is like, see, and except we made a
variable of type I.F. stream, right?
So just imagine this is a stream, but it's coming from the file that we opened and now it's flowing
into our variable and gets stored in our variable.
So we're going to head to the editor and make a small example program.
So here you notice I have a stream right here, so I've included that already.
So what I'm going to do is go ahead and make a little integer variable, and I'm just going to call
this my end and then I'm going to do seed Conklin Extreme.
I'm doing the second call in because I am not doing a using name space eight.
So if you are, you don't need to do this part right here.
You also are going to notice that I have a few more suggestions here.
So if I say I have a stream, it's like giving me a lot of options.
That's because I actually just recently.
Downloaded the little add on that I had, so, you know, I had like a little C++ extension or something
that I added on, so it's giving more C++ recommendations and stuff.
You don't have to do that, but you could if you'd like.
So just wanted to point that out as well.
So I make an extreme, I'm going to call this my stream and then what do we do next?
We're going to open it right?
So I do my stream dot open and I'm going to actually put something called input file tucked in here.
Why am I doing that?
Well, let's see what that thing is so I can actually use a cool command and this is available in PowerShell
or Bash if you are a Linux user out there.
You can use Cat.
So I'm a new cat and then what I'm going to do is input file to that text.
What the Cat Command does in this case is it's going to show us the contents of that file.
So it's going to show us all the data that's in that file.
I press enter and you notice that that file contains one space, two space three.
So it's got three integers in there, right?
This command has a lot more to it.
If you're really interested in this, you can go look it up.
You can do a lot of cool stuff with it.
It's a really neat command line kind of tool that you can use.
But right now we're using it just in the sense of wanting to see the contents of a file, right?
It's going to print out the contents of the file that we use it with.
So Cat Input File two shows us what's inside of input.
File two.
So just a little kind of side note there.
So that's what we're reading in from.
I'm going to go ahead and then do a my stream and read into just like, see with this info operator
into my end.
And then just to double check, what I'm going to do is I'm going to see out and I'm going to see out
my aunt.
I guess I don't need that.
And then we'll obviously combine.
OK, and one last thing we have to do, I can kind of go back to the presentation here, but we need
to do a mainstream foreclose and why do we do this?
Well, we've opened a input stream.
We kind of need to do a little cleanup process and close it.
So everything is all good to go.
So just remember to do a mainstream foreclose once you're done working with the input stream, once
you're done reading from the file.
All right, so let's go back here and let's finish up with that.
So I'll be on my stream that close.
OK, so pretty cool, right?
I'm going to go ahead and save this and what do you think is going to print to the console if these
are the contents of our file?
We'll go ahead and compile lists.
So how do you file stamped on that CBP dash?
Oh, that's one that you see.
And let's run it, so what do you think's going to print out?
Well, it only printed one out, and that is because we only read one integers, so it saw this first
integer, then there was a space, so it stopped and it just read that into my end and then it printed
that out.
So what if we wanted to read in all three, I can make some other integers, right, so I can say aunt,
my aunt too and aunt, my aunt.
Three.
Sorry, my typing is still a little off because of the wrist thing, but then what I can do is I can
go ahead and read into all of these so I can just say my aunt to and my aunt three.
And then right here I can also print these out.
So I'm just going to copy paste this and I'm not going to print it out with any spaces.
So it's going to be all smashed together.
But that should be OK.
So now what do you think it will print out?
So it's reading this in all spaces, but not printing out spaces, so we should get everything right.
So I'll go ahead and save this and compile it and run it, and we get one to three all smashed together
because we've read all three of the files that were in this file right here, not the files with all
three integers that were in the input file 2016.
So pretty cool, right?
We know how to now read in from files.
So let's go ahead and mess around with some output streams stuff.
So let's check out what that is.
So let's head back to the presentation.
Output streams are declared like this, so rather than IAF stream, it's off stream, so not input file
stream, but output.
Oh, for output file stream, right?
So when we want to write to a file, we do very similar things.
But instead of using the info operator, we will use the output operator.
So this is the input operator here.
So that's what we would use with like CNN, right, and then we will now want to use something like
we would use with the out.
So here I have an example.
This is a little redundant, it's printing out my integer, but we don't care about that much because
we're wanting to probably just check to see that it successfully got written to the file.
We declare an output file stream or assuming that somewhere up here we made an integer my end and we
read into it with an input, right?
So we read into it with an input file stream.
We then open the output stream that we created and we're going to do a similar thing.
We're going to put the file in here that we want to write to this time.
Then what we do is we have the data from the integer variable flow in this direction.
Into the output stream, which will then write it to this file output.
Here we kind of print it redundantly.
We don't need to do that because we can just check to see what's in the file with cat, right?
Then we wrap up by closing the output stream just like we closed our input stream, right?
So let's go back to the editor and see what's going on with this, so I'm going to just add some stuff
in here.
I'm going to say I see Colin Colin 0f stream and I'm going to say my out stream, and I'm actually going
to change this to my in-stream and I'm going to need to update this in a few spots.
So this will be my end, not open.
Be my in-stream right here, and it will be my instinct close.
So what I'm going to do now, though, is I'm just going to open my file down here, my output stream.
I must say my Allah stream that open and I'm going to just say output to tea because I have input file
to write some and a similar naming convention.
And then what I'm going to do is just make it out to their right.
So I'm going to put this operator right here is that you notice is similar to like when we you see out
and all I'm going to do is I'm just going to copy paste.
This stuff right here, right, so I have one operator, so I'm going to do that and I'm actually going
to put a little on, let's just say like, I've got a couple new line characters here or something.
So we have a couple of new lines.
And that should write it out to our stream, and then all I have to do is close, right?
Some of my output stream got close and we should be good to go.
I'm going to go ahead and save this.
Let's go ahead and compile and run this, and it's going to print out, right?
And then what we want to do is verify that the same thing that gets printed out right here is actually
what's inside of our file.
So we'll use the command again to verify that.
So I run it, it prints out one two three and then what?
I can use cat output to that text.
And we noticed that we have one two three.
But to differentiate it a little bit, you notice there's two new lines, and that's why you noticed
these two new lines.
Unlike when we just ran the program, right when we ran the program, it just said one two three.
Now you notice the prints out to new lines.
So pretty cool.
We now know how to write to a file.
I have another example file here, though.
Go ahead and clear this and I'm going to count this other file.
It's just input file that text.
You notice that it actually has like a little sentence, a string and says here is an input file.
So what if we now wanted to read into a string?
So I'm going to go ahead and mix this up here and put a silly string my string and I'm going to read
into my strings and I'm going to say my string.
And then I'm not going to print this out any more.
What I'm going to do is just read into there and then I'm going to put it into my Airstream, so I will
say my screen.
And now we can read in the stream, right?
So should be pretty cool.
Let's go ahead and run it and see how that works.
So I go ahead and I run it, and then let's go ahead and cap the output output 2.60 or this time.
I actually sorry, I need to rerun this because I realize I did not change it to amplify that.
So let's go ahead and rerun that.
So I saved it.
I'm going to compile and I'm going to rerun it because we still had input file to this time, I want
to read from that input file without the two that has the sentence, right?
So I run that and then now let's go ahead and see what's in our output to that text.
We had one two three in there, right?
And then two new lines.
Now let's see what's in it.
So you notice it just says here and it has some funky things for the new lines here.
If I was to go into a dash and I left the cat the outfit to the text, we noticed we see the hue with
the new lines in there so we can verify that it now has changed, right?
But the thing is, we just read the word here.
We really wanted like this whole sentence, right?
But we only got this first one.
Why do we only get the first one?
Because strings just read until whitespace by default here.
So we read the here we hit whitespace.
It stopped, and that's all it read in.
We do know how to read a full sentence, though, right?
And that is using get line.
And the cool thing is we can totally use get line with this input stream, just like we have with scene.
So let's go ahead and do that.
I'm actually going to get rid of these instances and open a new terminal
and let's go ahead and modify this.
So rather than reading into this right here, I'm actually going to do a speed control and get line.
And in here, I'm not going to do scene and my string like that.
I'm actually going to replace C rn with my in-stream right.
So my end is going to be right here.
So we're reading from that file.
Let's go ahead and save.
And after saying here, I'm going to go ahead and compile.
So far, Change Wanda, CVP show this one, the BBC, and now I'm going to what it looks like we have
a.
Problem within my in-stream.
Foreclosed.
So I just didn't.
Actually, it's with this, I just did not put a semicolon.
So let's go ahead and try that again.
Now I run fs one.
You see?
Oh.
So I run that, let's go ahead and can't output to that.
So you notice now it has the full sentence and I can go into bash.
Kind of get these weird characters for the new lines there.
But if I go over here and bash, let's go ahead and cat output to see.
Hopefully this shows up in there.
So it says here's an input file.
We've read everything into it now, so we can do that.
Now I logout, bash and back into PowerShell.
So pretty cool, right, we can actually read in with get line and we can just kind of standard read
in from the stream like that as well.
So this is all that I am going to go over right now.
But what we're going to get into is actually using loops with the stream as well, so.
Think about that because I'm going to have a problem in the next lecture, we're going to learn kind
of one more new concept and you could use a loop and have something like my in-stream into my string
and you could just keep reading strings.
Imagine this is inside of some wow loop or something like that, right?
So that is something that we're going to look into.
And with that, I will go ahead and see you in the next lecture.