Hello, and welcome back.
In this lecture, I'll provide a demonstration for our previous challenge,
which is printing the content of a file in reverse order.
So you're going to need to understand the ftel method and the fseek method.
Let's bring up code blocks, and let's create a new project.
And this project is going to be a console application.
We're going to just name it
print file contents
in reverse.
And we're going to first create our file that has some empty data in it because we need to
read from it. So I'm going to go back to my downloads directory, which is where my
code blocks projects are.
And I'm going to go to the project that I just created,
which is named
print file contents.
So it's right here. I'm just going to create a new file, text file.
We'll say new text document. We'll name it test.text.
And then inside here, I'll just do the normal hi my
name is Jason,
how are you, I am doing fine.
And what this will do, I'll save it, and then we're going to start printing out the end.
So it should print out enif space gniod and so forth until it gets to the beginning.
So we'll go back to code blocks,
and we're going to need to include
our standard IO because that contains all of our functions. We're going to create a constant,
in this case define.
And this is going to be called file name, it's going to be called test.text.
And I'm going to need to create my file pointer first, so all caps FILE
star fp, and I'm going to set that equal to null, initialize it.
And then I'm also going to create a couple counters
and something to represent what's returned from --
I'm going to carry the counter for my loop and then
the count of the actual bytes that is returned from ftell.
So I'm going to say it count equals zero
and then a loop control variable into I equals zero.
I'm going to do my normal. I need to open up my file. I'm going to say fp equals fopen,
and I'll pass in the file name. I'm going to pass in read only so just r.
I'll make sure this is not null. So I'm going to say if fp equals null,
then I'm just going to return a negative 1.
Otherwise, I'm going to seek to the end of the file.
So I want to do an fseek.
Here I'm going to call the fseek function.
This is going to change the position of where I'm pointing to. I'm going to pass in my file pointer.
I'm going to pass in zero for our second parameter.
For the third parameter, I'm going to say seek to the end.
So I'm going to use that constant seek end.
And that's going to set my file pointer to the end of
fp, which is test.text.
Once I'm to the end, I want to set my actual counter
to what it's pointing to is ftell. So I want to get back where I am.
So here I'm going to get the position of the file pointer.
And the fseek actually changed the position,
so it moves the file pointer to the end of the file.
So I move it first and then I get the position. I can get the position using ftell.
For ftell, it returns the position, so that's going to be stored in count
and I'm going to say f count equals f tell and then pass it in the file pointer.
Once I have the position, I can then write a while loop that says
go from the position or the counter which is I my loop control variable all the way to the end.
So here I can say while I is less than count
because I want to basically read in everything from the end to the beginning.
And so inside here, I want to say increment my I variable.
And then I want to seek, I want to change where I'm moving to. So I want to say fseek,
and I want to point to fp.
I want to point to a negative I
because I'm going from the back to the beginning, and I want to say seek end.
So negative I is going to be so many bytes from the end.
So first I was able to move to the end
and then I want to change my position so that I can start reading in each character.
Once I've actually seek to the end, then I can just
print out the character,
reading the character and print it out. So here I can just say percent c,
and then I can say fgetc and
my file pointer.
So again, what I'm doing here is on line 22 or line 19,
first, I'm moving my pointer to the end.
Then I'm getting my counter because that will tell me how long I have to read in each byte
using ftell.
Then my while loop is going to go from zero
all the way to count, how many bytes I want to read in.
On line 26, I'm going to increment the counter.
I'm then going to seek to the so many bytes from the end,
which is minus I from the end. And then lines 28, I'm going to read in that character and print it out.
And that's the main logic for the program.
The only other things I have to do is I want to print out a line feed at the end,
just to make it easier to read. I want to close my file pointer.
So I want to say fp, and then I just want to return,
and I probably want to set fp equal to null.
So let's try compiling this, and what we're going to see hopefully
is the file print in reverse order.
We're not changing the contents of the file,
we're just reading it in from the end to the beginning using our position functions.
So when we run this we should see the opposite
or the reverse order. And so what you'll notice here is we see the reverse order.
So we're first printing out the last file.
And if we bring up that file in our downloads directory,
you'll notice that
it's printing out as output.
It's printing out
enif space gniod and so forth.
Last line that's printed out is hi my name is Jason in reverse.
So it's just a neat little program that will
use the file pointers to start at the end
and then go to the beginning and print out the contents in reverse order.
So notice how we use fseek, notice how we use ftell.
Fseek moves the file pointer, ftell actually gets
where you are in the location of the file.
So now you can use the count to go in reverse.
And then again, use negative I to say how many bytes from the end
and fgetc with a file pointer.
I'll provide this source code as a solution,
and you can compare it and look at it and analyze it in more detail. Hope this helped. Thank you.