Hello, and welcome.
In this lecture, I want to provide a solution to our previous
challenge, which is converting a file that contains lowercase
characters to a file that contains uppercase characters.
In the meantime, we're going to create a temp file, so we're going to need
to rename and delete and so forth.
And the final output for the program is going to be the file.
We're going to read in the file that was already converted to uppercases.
So let's bring up code blocks, create a project.
I'm going to say new project, console application, go c and this is going to
be called converting lowercase file.
We're just going to have this be our main.c.
We want to make sure that we create our standard io header because we're
going to be reading and writing and using fopen and so forth.
So what I always like to do is create the file right up front.
So here I'm going to define a constant for my file name.
Let's call this define.
And this time we'll call test.text.
And so we'll just say file name is all caps.
I like doing that convention.
And then I'm going to actually create the file name in our project.
So again, I store my files inside my downloads directory.
That's where all my projects are stored.
And in this case, we have a project called converting lowercase file.
I'm going to put it right in the same directory, so
I can run it from the IDE.
I'm going to create a new text file.
I'm going to name this test.text.
And I'm going to bring it up, and I'm just going to put some
lowercase and uppercase characters.
So let's say Jason is testing. Okay, whatever a test some are
uppercase, some are lowercase at the end result, these will always
be uppercase, another challenge.
There we go.
So the end result of this files is going to be test.txt
file with all this uppercase.
But we're going to save it with some uppercase and some lowercase.
Now we have our file created now we can actually do
some reading and writing.
I'm going to create a couple file pointers with file all case uppercase
file, and I'm going to say star fp.
I'm going to initialize that to null, and then I'm going
to create another file pointer star fp1 and also equal to null.
Again I need two here because I'm going to do reading and
writing to separate files, a temp file essentially.
I'm also going to create a character variable, which will be the character
that we read in using fgetc.
And I'll just leave that also equal to null as well or better yet,
I'll just leave it empty string.
The first thing I need to do is I need to open up the file in read mode
for the first file which is fp and the second file will be a temp file.
So I'm going to say fp equals fopen.
I'm going to use the file name test.text.
So file name, and I'm going to open up for read only.
And then I want to make sure that this is a null.
So I'm going to say if fp equals null, then I'm going
to just return negative 1.
Then I want to create my temp file.
So here I'm going to create a temp file.
And in this case I'm going to open up a file for writing.
So I'm going to use my fp1 pointer, and I'm going to say fopen.
This one I'll just call temp.text, it's going to
create it if it doesn't exist.
And then I'm going to put the w because I'm going
to write to it write only.
Again, what I want to do here is I want to make sure
fp1 is not equal to null.
So I'm going to say fp1.
If it equals null, then I'm just going to return a negative one.
So now I opened up two files, one for reading, one for writing.
The writing is for the temp file.
And what I want to do next is I want to start reading from the file and
then writing all the uppercases.
So I want to add a while loop to read and I want to say while the
character data type ch is equal to fgetc, and I want to read from my
test.text, so fp and I want to do this until I don't hit the end of file.
So I can say not equal to eof.
Eof is a special constant.
Inside my while loop, I want to check if any of the characters
that I'm reading are lowercase.
So I want to say if is lower ch,
if I find a lowercase character,
then I just want to set the character to be uppercase.
So I can say ch equals ch minus 32.
This will convert the character to an uppercase character.
Then after I've converted it, either it's converted or not either,
it's uppercase or not, then I want to write it to my temp file.
So here I can say fputc.
And I can just say my character that I want to write and the
file pointer which is fp1.
So every time I read a character, I'm writing it to my temp file.
And if it's lowercase, I'm converting it to uppercase and then writing it.
So the end of this while loop is I have a temp file that contains
all the characters from the test.text with uppercase letters.
And so I'm done with both of those files reading and writing.
So I can say fclose fp and fclose fp1.
I want to now rename my temp file to my test.text file.
So here I can just say rename, call the rename function,
and I can say temp.text that's what I want to rename.
And the one that I want to rename is test.text or use my
constant which is filename.
I still have my temp file created.
So I want to delete the temp file.
So remove the temp file.
Here I can call the remove function and pass it in temp.text.
And then the last thing I want to do is I want to print out the
contents of my test.text file, which will be converted to uppercase.
Instead of closing fp because that's pointing to test.text, I
could not close it now, I could just close fp1 because fp1 was
the temp.text and that's the one I'm renaming and removing.
Actually I have to close it because I renamed it.
So I'm going to have to close fp.
So now on line 43, I want to reopen fp because now I want to read
the contents and print it out.
So I can say fp fopen file name or sorry file name and I want to read it.
So I'm just going to say r.
Again, I want to make sure it's not null.
And if it's equal to null, I want to return.
And I just want to say return negative one.
I could print out some input.
And then here I want to read the data and just print it out.
So I can do another while loop.
We haven't printed anything yet, so we should be able to just say while ch
equals fgetc fp and not equal to eof.
And what we want to do is we just want to print out ch.
So we can say percent c ch, and then we're all done.
And then again, at the end, we want to close that last file which
is fp and we want to set fp equal to null and fp1 equal to null.
So let's make sure this builds.
So we're good to go.
We're able to compile, put a semicolon in the line of 54 I had a colon there.
So if we go through back through this code, we create
a couple file pointers.
We have our constant.
We open up one file.
We read from it.
And as we're reading, we write it to a temp file.
We then delete the temp file.
We have to delete both the file we're reading from and the temp file
because we have to rename and remove.
We can't call rename or remove on a function or on a file that is already
open, so we have to close them.
So then we reopen and we print out the data.
So we run this.
We should see the output with all character information from.
So when we run this program, what we'll see is it prints out
everything in capital letters.
So again, we run it, it prints out everything in capital letters.
If we look at the file on disk, we can look at test.text,
and we'll notice that it now contains everything in uppercase.
Again, if we wanted to change this and change some more lowercase.
So we save it here is lowercase we run the program and it
converts everything to uppercase.
Again, if we run this, print it out as uppercase.
We look at it and it's all uppercase.
So a nice little program that will convert lowercase
characters to uppercase.
Pretty straightforward, we're using our important functions
here, which is file pointers fopen.
And then we're using nice fclose remove and rename.
And then the fopen and fgetc.
If you have any questions, please email me.
I'll save this source file as zip file so you can take a look at it.
Thank you.