- In this video, we will take a look at
the second type of files, namely random access.
Random access files are different than sequential files
because they only allow access to the data
at the point at which it should be read or written.
You can look at it as being able to
go directly to a location, skip everything else,
as long as you know how many units to skip.
Typical operations where these files are,
open the file in one of several modes.
Just like sequential, you can open it
for reading, for writing, or for both.
You can obviously close the file.
You can advance to a specific location and read from file.
If we can make an analogy of sequential files
to the old tape decks, those of you who remember the tapes,
in order to get to the third song on the tape,
you had to fast forward all the song, the entire material,
until the beginning of the third song.
With dynamic, this is a little bit along those lines.
You can instruct the reader, the pointer,
to advance directly to that position, and this is because of
how memory is being managed internally using pointers.
We can go to a specific location and either read or write.
In this file, we are going to look at two examples.
One is a function called readAndVerify,
that will write some content to a file,
and the second one is add text to location,
where we will first write some content,
and then we will overwrite it with additional content.
The first one, readAndVerify, is defined
in a very similar manner to the one that we used
in the example with sequential files,
except we have a slightly different set of functions
that are at our disposal in C.
First, the fopen command is similar
as it was with sequential file.
We're simply opening a file.
In line 34, we are calling a file in a similar manner,
and the directive this time is w+,
meaning we are opening this file to read and write.
We are going to put a message into the file
using the command fwrite, for file write.
The message, in line 35, it's messageToWrite,
which is a string or an array of characters
defined in line 28 that says,
This message will be written to a file.
We are writing the message in the following manner.
We are telling the fwrite function three things.
One is what is the message,
the other is what is the length of the message,
and finally, to which file to write it.
That's the fp.
The length of the message is given to us
by the function strlen, which is available
because we included the string dot age library.
Finally, we are using a new command,
which is specifically for random access files.
It's called fseek.
Fseek essentially tells the pointer
where to point in the file.
In this case, we want the pointer to go to location zero.
So, by telling fseek take the file fp, SEEK_SET,
set the reading head, or the pointer head,
to location zero, which is the very beginning of the file.
So, we wrote the message to the file in line 35.
We moved back to the beginning of the file,
and now we're going to read into a text buffer,
which is actually another array of characters,
the message that was placed in the file for verification.
Since we know which message was written,
we can easily verify that the message is the same,
and since we know that this message
has been stored in line 28 in an array,
all we need is to take the same length as we used in 35,
line 35, that says strlen(messageToWrite).
We have to take the same length of characters,
and this will be our verification.
Let's run this code and see what happens.
First of all, let's convince ourselves
that the message file, which we opened, is empty.
Because it is empty, whatever will be written
we know it's a result of this code.
I'm going to close the file and run our code.
This message will be written to the file.
This is the message that was printed by line 44,
because on line 42 fread read the message,
or the content of the file, from the beginning of it,
placed it into text buffer.
We then took the content of text buffer
and printed it on the screen.
For further verification, let's open the file message,
and recall that it was empty a few seconds ago.
Now we have the content in the file.
So, we created the mechanism to read and write data
to a file using random access.
I'm going to clear the console,
and now we are ready to proceed to the next function.
I'm going to now comment read and verify,
and I'm going to enable add text to location.
Let's look at this function first.
This function is going to write a message to the file
using a slightly different method this time.
We are opening the file in the same manner
and using now the command fputs.
If we're going to print, not much to say today,
to a file using the pointer fp,
then we will go to location eight.
So, if you look at here, this is zero, one, two,
three, four, five, six, seven, eight, okay?
We'll skip eight location.
Again, we SEEK_SET, and we'll replace from that point and on
the existing text with the text in line 55, to talk about.
So, the original text would be, not much to say today.
It will be replaced with, not much to talk about.
First, let's clear the file
so there is no content from before.
I'm going to save this one.
And let's run the program, and let's see what it does.
So, this time I'm not outputting anything on the screen,
so there's nothing here,
but I'm going to look into the file,
and it should say, not much to say today,
and then it would have been replaced by,
not much to talk about.
So the final outcome is, not much to talk about,
and here it is.
To wrap things up, we looked at dynamic files.
We looked at sequential files, random access,
which gives us a lot of flexibility.
There is not one situation better than the other.
There's not one tool better than the other.
You now have several tools that allows you
to write content to a file,
either sequentially or through random access.
You can read it.
You can use file to save data and so on.