- In this video we'll discuss
Data Management Using Sequential Files.
First, let's start with defining what a file is.
It is simply a named portion of storage area.
In the same manner that you label
a sequential set of memory locations
and call it an array,
you can do so and call them a file.
A file is a slightly different
structure than an array.
It is a more complex type of data storage
and we should be accessing it in a different manner
as we will see shortly.
Files are of two types.
They are random-access and sequential.
A sequential file means that, for example,
if you want to read the 20th character,
you must first read all the first 19th, to get to the 20th.
You cannot jump directly.
You have to open the file in one of several modes.
You can open it for reading, for writing.
For example, you can close the file so that
it is no longer accessible to the program.
You can read from the file
until a desired location is reached,
or you can read the entire file
and you can write to a file
when arrived at the desired location.
We are now going to see how this is done in practice.
In this example, we are going to look at three functions.
One, line 15, reads the entire file.
The second one reads until a predesignated marker
and the third one is going to show how we write
to a sequential file.
I commented out, line 22 through 24,
because I want to focus, for now,
just to read the entire file.
For this exercise, I created a text file
and I placed it in the project folder.
Let's open it first.
The file contains "This is a message."
I'm going to close the file right now
and we'll see how we can read it with this function.
Read the entire file.
First of all, let's focus on line 31.
Line 31, I'm defining a pointer
to a data type file.
We need this pointer because this is the way
we will refer, from now on, to that file.
In this particular instance, line 32 shows
how we can open that file
and establish a link between the pointer
and that particular area in storage.
For example, fopen, the full path to the file
and at the end, an "r".
This designates this file
as being open for reading only.
I now proceed, line 33,
to read one character from the file.
If you recall, we had the ability to read
from the screen using the getchar command.
The counterpart of that, when it comes to file,
is the getc command.
It's actually a function that requires a parameter
and that parameter is fp, the pointer to the file.
A program could have multiple files open
so we have to tell it to which one are we referring to.
In this case, the int defined in line 30,
ch, or for character, is the one that will hold
the output returned by the function getc
that reads from the file message.
We shall now start a loop in line 34
while ch does not equal End Of File.
End of File, it's a predefined constant,
it is actually -1.
If you hover over it in eclipse,
it'll tell you, it is actually a Macro,
but for readability, we call it End Of File.
We are first printing on the screen
the first character which was read outside the loop.
We read the next character
and continue looping through the file
until we reach the end of it.
If the file was empty, nothing would have been printed
because the loop would not have started.
Then we close the file.
It is a good practice to close the file
to avoid any system hang-up.
Let's run this program and see what happens.
Indeed the file was read, "This is a message." as intended.
Let's move on, and now I'm going to do both things.
I'm also going to read until a marker.
This is essentially the same function,
except I'm passing it a value of a marker
which is, in this case, the letter "m".
The only change is line 46.
Instead of reading until End Of File,
I'm reading until the first occurrence
of the letter "m", which is the value of marker.
Let's run this.
As you can see, everything was read until "m".
Finally, we no longer need those,
instead, I'm going to write to file.
Let's see first, to which file am I writing.
I'm actually creating a new file
because if you look at line 55,
I am writing to a file named message2.
If you look at my project on the left-hand side,
there is no file message2.
When this program execute, if there isn't such a file
it is automatically being created.
In addition to that,
I'm going to use another type of command.
If you recall, in line 57,
we used printf to print on the screen.
fprintf, it's short for file print f,
which is formatted file printing.
It works the same as the regular printf
except, we have to provide it a pointer
so it knows to which file to write.
In this case, fp is pointing to the file message2
and you notice at the end of line 55,
this time I'm opening the file with a "w" for writing.
I'm going to run the program.
Nothing happens on the screen.
We don't see a file yet,
so I'm going to right click and refresh
and here's my message2, "This is the message for Roger".
Why does it say message for Roger?
Because I printed, "This is the message"
and then a name, which in line 56,
is an array that contains "Roger".