Now let's look at how to read the input.
Back in ands it's going to be a little bit more involved.
So for this we're going to use the I am stream object of stringy ice cream type file
in just like before we left you open the file
read back in test text that we made last time
and here we're going to check to see if we could open the file.
So how you do as you say is open like this because what if maybe the file doesn't exist.
So you couldn't open it.
In that case.
So we always have to check to see if our files open.
We should be doing this with her out files as well.
So here now what we want to do is actually figure out how big the file is and and you can really think
of files as one big long array and that array will have a cursor where to where it currently points
to.
So you kind of think of it as one big one big array of bytes and the cursor really points to the current
byte.
So when you open the file the cursor is at the very start of the file.
So we want to essentially move the cursor to the end of the file.
Ok so how you do that as a Sikh.
JE zero in five dot and.
So this line will actually move the cursor of the file
to the end of the
Am i asking why you want to do that.
OK.
And this is the reason.
So here now we can actually get how big the file is.
In general I saying infile dot TLG.
So this is actually what TLG does will return the position of the current character in The File.
And since we know that it's at the end this will essentially give us how big the file is and bytes because
it'll start at the first bite at the start.
When you open the file and then you'll seek to the end of the file.
Ok so the cursor is at the very end and TLG will actually tell you what what position the current character
is in the file.
So each character is one byte.
So this will actually give you the entire length of the file.
So now we want to actually read from it again.
So we should actually put the cursor back to the start that by saying zero in filed the beginning.
This will move the cursor you the starch and this will again give the current character or the position
of the current character.
So hopefully this makes sense what's happening here.
So we're moving the cursor of the file every file has a cursor to the very end of the file.
OK.
And then we're getting the current position of the end of the file which really just gives you how many
bytes there are in the file.
This is what this really does.
Second line.
And then we're just moving the cursor back to the start of the file.
And we did all that because we needed the length of the file and we need the length of the file because
we need to know how much to store in our array.
And we're going to make it dynamically and B link plus one can and then using the infile again.
We'll use the get method and actually use this get method for in hangman if you remember to get an entire
C string.
So that's essentially what we're doing here.
We're just getting this E-string of this link.
So this is actually put all all the contents of the file into our new buffer that we created.
So this is the power of dynamic memory.
We have no idea how big the file is in the first place but we can figure it out.
And then just create a buffer to or an array to store it all day.
And then once we get it we'll just put it and this time we actually will use out.
And if you remember that actually we'll put the null character in at the end.
And that's why we have length plus one here
and I'll put it in and we'll actually just delete our buffer array and we'll close the file here.
So if we run this hopefully this will actually output what we input into the file last time we run it
like this.
Hello file.
So.
So hopefully all this made sense and that's a little bit more complicated.
But really this just gives you the length of file or how many bytes the file is.
There should be no surprise to you right.
We're just creating a buffer to put all the characters of the file into.
So this will store all the characters of the buffer and we just read it.
And this is what this getline does.
And we just read
read all the characters in the file and then put all those characters into this buffer.
OK.
And then we just put the buffer delete it and then close the file.
So that's really it for reading.
OK.
And we'll be going over more examples of reading texts in in future lectures.
But in the next lecture I want to go over binary files.