Hello, and welcome back.
In this lecture, I want to talk about file positioning, basically
some functions that will allow you to change the position of a file.
So for many applications, you're going to need to be able to access data in
a file other than sequential order.
Sequential order is starting at the beginning and then going
byte by byte towards the end.
That's not always useful because you may want to actually go into the
middle of a file and grab data or at the end of the file and grab data.
It all depends on the application.
You don't always want to go from the beginning to the end.
So there are various functions that you can use to access
data in random sequence.
There are two aspects of file positioning.
One is finding out where you are in a file and the other is
moving to a given point in a file.
Those are really the two things you want to find out when you're talking
about files and the positioning of a file, what it's actually pointing to.
Again, where you are in the file itself and whether or not you want
to move somewhere else in the file.
So you can access the file at a random position regardless
of whether you open the file, you can move your file partner.
You don't need to open up the file to change where you are
to change see where you are.
So finding out where you are we're going to talk about first.
You have two functions to find out where you are in a file one.
Is called ftel and one is called fgetpos, fget position.
For ftell, the actual function definition is pretty straightforward.
You return along and you take a file pointer.
And this function is going to accept a file pointer as an argument and
it will return the long integer value that specifies the character
position in the file, so it's going to tell you the position.
You can use it by creating a variable in this case fpos, which
is going to what's going to get returned and then passing in that
file pointer and calling ftel.
The f position variable now holds the current position in the file
and you can use it to return to the position at any subsequent time.
So it's going to return you fposition.
This is the way you can get the position of the current
point where you're pointing to.
The value is the offset in bytes from the beginning of the file.
So ftell very straightforward to use pass in your pointer it's going
to return and tell you where you are in your file based on basically
from the beginning of the file until how many bytes from the beginning.
So in this example, we're creating a file pointer called fp and we're
creating an integer called length.
We're going to open up the file, and this would be for reading only.
We're going to make sure it's not null.
If it is, we're going to return an error.
And then don't worry about the fseek.
The fseek is actually just going to go to the end of the file.
This is another function that helps us.
I'm only using it to demonstrate the ftel.
Fseek is going to go to the end of the file, and then you're going
to say ftell fp, and that's going to tell you you're basically going
to be at the end of the file.
So the link that's returned is at the end of the file.
And so what's going to happen is it's going to return the total
size of the file based on length because you actually moved your
pointer to the end and then you said where am I and it said you're
at the end, so return the end.
And the end is the number of bytes from when you last read.
So let's take a look at this inside of our nice IDE our code blocks.
So here's our coding example where we create our file pointer.
We set it to null, make sure it's not null.
We try to open it up.
And then we do an fseek which is just going to change our file pointer.
Ftel is going to tell us where we're pointing to.
So it's going to point to the end and it's going to be any
number of bytes to the end.
When we run this, what you're going to notice is it displays 24 bytes.
So that means you're at the end of the file because the file is a small file
and the file only contains 24 bytes.
Typically, you'll use ftel after you've done some writing or
reading or after you did it in fseek where you moved somewhere.
But it's very simple to call ftel with the file pointer and then
len just tells you where you are.
You can also use the fget position or fgetpos function.
And this is a second function providing information on
the current file position.
It's a little more complicated than ftel.
This returns an integer.
It takes a file pointer and it also takes the f position t type.
So the first parameter is that file pointer, so you know
what file you need to read to.
The second parameter is a pointer to a type that is defined in standardio.h.
The f position t type is able to record every position within a file.
So fget position function is designed to be used when the
positioning function fset position.
So you use fset position and then you use fget position
in combination with that.
So the fget position function stores the current position and
file state information for the file and position and return zero if the
operation is successful, returns a non-zero integer value for failure.
So you can create a variable of s position type.
And here we say it's just here.
Then we call fget position.
We specify in our file pointer, and we specify in our f position type.
We have to pass in the ampersand, the address of because it's not a pointer.
So in this case, the current file position is in the
variable hello or here.
So you have to declare a variable of type f position t because
that's what you pass in your parameter, your second parameter.
You can declare a pointer of the type f position t star because
there will not be any memories associated to store the position data.
So let's look at an example this will make a lot more sense.
In this case, we create our file pointer, we create our f possession
t type position, we call open and then we call fget position.
And we pass in our file pointer in our end position.
So that's where we're storing the position.
We're going to get the position where we're at.
Then when we say f put s, we're writing some data
to wherever we move to.
So again, f get position and ftell tells you where you want to
actually write to or read from.
It's somewhere in the middle.
It's not at the beginning, it's not at the end.
You're moving.
You'll usually move with a seek or your move with something
else, get position and that tells you where you're at.
So fget position and ftell is going to tell you exactly where
you're pointing to, and that means you can move somewhere
else and you'll be able to read.
Now setting a position in a file up until this point, we're
just reading in where we're at.
We're not going to really read in where we're at unless we set
something, we did a read and write.
So as a complement to ftell, you also have fseek.
And fseek is going to allow you to move somewhere for your position.
It returns an int.
It returns a file pointer, a long fset and an integer original.
The first parameter is a pointer to the file you are repositioning.
The second and third parameters define where you want to go in the file.
The second parameter is an offset where a reference point
specified by the third parameter.
The reference point can be one of three values that are you
specified by the predefined names.
Seek set, seek current and seek end.
Seek set defines the beginning of the file.
Seek cur defines the current position in the file and seek
end defines the end of the file.
So for a text mode file, the second argument must be
a value returned by ftel.
So you should notice that with fseek and ftel, they're used together.
So the third argument for text mode files must be seek set.
For text files, all operations with fseek are performed with
reference to the beginning of the file for binary files which we
haven't really learned much about.
But for binary files, the offset argument is simply
a relative byte count.
You can, therefore, supply positive or negative values for
the offset when the reference point is specified as seek cur.
So let's look at an example.
Here in this example, again, we do the fputs.
So we write some data to our file, and then we use fseek.
Fseek is going to move you.
Here this is saying you're going to move seven bytes and
we use seek set to say we want to move somewhere basically
seven bytes from the beginning.
And we can move from the beginning of the file by seven
bytes that's going to move it.
After seven bytes, we can then actually write data to it.
And we would also be able to use ftell to see that oh after
seven bytes where we are.
You can use the information to retrieve back from where you are.
Again, the whole point of the fseek and the fgets is you're either seeing
where you are located to specify where you want to write to or read to.
So fseek s is actually going to move the position, tell is going
to tell you where you are located.
Fset position is also like fseek.
But fset position is a little more complicated.
It will go with the fset position.
So fset get get position is also very similar to fset position.
With the fset position, it specifies the first parameter
as a pointer to the open file.
The second parameter is a pointer of the f position t type.
The position that is stored at the address was obtained
by containing fget position.
So fset position has your file pointer and then your actual variable.
And the variable here was previously set by a call to fget position.
Again, you're going to use fset and fget together.
The fset position returns a non-zero value on error or
zero when it is successful.
This function is designed to work with a value that is
returned by fget position.
So you can only use it to get to a number in a file to get a place in
the file that you have been before.
Fseek allows you to go to any position just by specifying
the appropriate offset.
So here we look like we have our f position t type position.
We open up our file and then we call fget position.
In fget position, we're specifying where we want to move the
pointer, where we're actually reading or writing in the file.
Then we use fputs to wherever we moved it to.
And then fset position is going to again set something specifically
where we want to write to and then write it from wherever we want.
So with all these functions, you want to actually see
where you're pointing to.
This will become way more easy to understand once we have our challenge.
So with reading and writing, you always want to move somewhere to
the point of that you're writing to.
You don't always want to write for the beginning or the end.
And so these functions are very useful to move where you want
to position anywhere in a file.
Thank you