- In this video, we are going to summarize
some of the things we discussed so far
and bringing them together
in a read-write combined application
of a realistic situation.
Whenever you want to open a file,
you have to ask yourself,
what is the main action you want to do with that file.
If all you want is to read and want to prevent
inadvertent overwriting of the material,
use the directive r.
If, on the other hand, you don't have a need
to read anything, or you just want
to create a new document
that didn't exist prior to that,
you can open a file for writing,
and the fopen command with a w directive
will create the file if it doesn't exist.
Sometimes you would like to write a log.
In this case, you want to make sure
you're not overwriting existing content,
and you are forcing it to be appended
at the end of the file.
A+ opens a file for reading and writing in append mode.
So you can read, but the writing
is only in the append.
R+ opens the files for both reading and writing
for maximum flexibility.
Let's look an application in code
where all of this will be used in the same place.
In this example, we are going to create
a copy of an existing file.
We have a little text file here called sourceFile
that has two lines of content.
As you can see, this is the first line of the file.
The second line does not add much.
Let's close it.
Then, I'm going to do two things.
I created a function called copyFromSourceToDestination
and a second one, appendCopyrightNotice.
I will keep only the
copyFromSourceToDestination open
so that we can verify the interim content of the file
before we perform the second one.
Let's take a look at the copyFromSourceToDestination.
It's a very simple loop like the ones you've seen before.
However, if this case,
we are using the fopen command twice.
One is to open the source file
for reading in line 30.
The other is to open the destination file
for writing in line 31.
Notice that the destination file does not exist.
It will be automatically created.
Once we open the file, we are starting
a reading loop until the end of file in line 33.
As we are reading the character,
we are immediately copying that character
onto the destination file in line 34
until we reach the end of the first file.
When we are done, we are closing
both files, 37 and 38.
Notice that obviously for this to work,
in line 29 we needed to declare
two pointers to files.
Let's run this and see what happens.
The program terminated, and I didn't ask it
to produce any output.
We don't see any destination file here
so we will have to Refresh.
The destination file has appeared.
Let's open the source,
and let's open the destination,
and as you can see,
they are absolutely identical.
I'm going to close both files again,
go back to the code,
and enable the appendCopyrightNotice
and disable the copyFromSourceToDestination.
We're going to run the code again,
and this was done very quickly.
Again, I didn't ask for any message to be printed.
Let's look at the destination file,
and now we see the copyright notice has been added.
How did this happen?
It's because we ran that function
appendCopyrightNotice,
which is simply opening the file
in append mode,
and is adding two blank lines in line 45,
followed by the copyright notice
and closing the file.
This concludes our discussion of files
in which you learn how to read,
how to write, how to write into random access mode,
how to read and write sequential files.