Welcome.
So we're now going to explain how to seek in files so the first thing I want you to do is include esta
de had a file to give us access to the file functions and what seeking allows you to do in a file that
allows you to seek to a position in the file and start reading from there.
So it's very good for seeking through large files that might be gigabytes in size for example.
So once you've done this I want you to create a new file here and we're gonna call it test ticker tape
and I just want you to type a bunch of data let's make shows a little long.
They are mine.
Nearly two thousand characters.
Now I'm back timing function we're gonna open this file for reading so I want you to do file asterisk
file equals f open and then I want to just say top forward says T X T and the mode to be read.
So that's a load test all t t from the from the local directory.
Now I want you to do X seek and it's gonna ask you for a string and an offset type and file for the
stream offset.
Let's say 20 bytes on the origin will be seek seek current.
So if we now go chop off 20 and we do f rate buff and we go 1 and 20 here and then we should be able
to do we should be able to output our buffer up from where we seek to.
But before we do that what I want you to do.
I want you to do both at just before this.
Sorry I want you to do buff 19 equals zero so this adds on null terminator because we're reading data
from the file here but because we're only reading a certain amount of data the null terminator which
ends the string is not included because the null terminator is all the way at the end of the file.
So if we now do print f percent s birth and we compile and run that with GCSE main that C Dasha Maine
and we run that we can see our string.
Now this is offset it 20 bytes into the file.
So essentially here when we do f seek we're changing our current reposition in the file and we're putting
it 20 bytes ahead of our current position.
That's what I seek.
See you all means so essentially by using f seek we can reposition where the file is pointing.
So in our case we say 20 bytes from our current position so you can use seek set to go from the beginning
of the file fee you sick seek set then accounts from the beginning of the files.
So in this case it will be the beginning of the file plus 20 whereas if you you seek.
See you off a current position then let's say we're we were already at position 20 in the file this
would then take us another 20 bytes ahead.
OK.
I'm now going to show you how you can use file seeking to determine a file size.
So if we get rid of all of this and we did f seek file and I want you to do zero here seek.
And so that's oppositionists at the end of our file.
And now to get the position we just do f tell
file.
Now if I haven't.
This is an unsigned long.
So if I go unsigned long costs equals FTL file.
Now this will give me the size of the file.
But obviously you probably want to go back to the start of the stream so that you can start reading
from the beginning right.
So to go back you just to f seek file 0 seek set and that'll bring you back to the start of the file.
So if we now do this
it should give us the size of our file and if we can pull that now we see one thousand three hundred
and five which is the size of our file and obviously this it this is named POS because it's the position
of the end of the file.
Right.
So you would call this file size if you wanted and maybe that's a bit more clear.
So that's how you can see in the file and move your position of where the the read and write heads are.