Hello, and welcome back.
We have another challenge for you regarding file input and output.
Specifically, I want you to write a program that converts
all the characters of a file to uppercase and write the
results out to a temporary file.
Then I want you to rename the temporary file to the
original file name and remove the temporary file name.
So you're going to be doing a number of things for this assignment.
You're going to learn how to both use the fopen function using temp file.
You're going to use the rename.
You're going to need to use the islower function to check for
whether or not characters are in lower case because we want
to convert them to uppercase.
So if it's lower you want to convert it to uppercase.
And we're also going to use the rename and remove functions, because we're
going to use temporary files here.
So you need to use the fgetc and fputc functions because we're going
to be using reading in characters because we're doing conversions.
You want to use the rename and remove functions because you only
want one file to exist at the end.
So you create your file, it has some contents in it.
Then you create a temporary file which you end up deleting,
but first you rename that to the original file.
So you want to create your temporary file as well.
And then you want to use the islower function
to check as you're reading in each character if it's a lower character
you want to convert it to uppercase.
You can convert an character to uppercase by subtracting 32 from it.
So if you do fgetc that's going to return to an integer character
or a regular character, char.
You can basically just call the is lower function on it, pass
in the char character data type.
And then you can basically say character equals character minus 32
and that will convert it to uppercase.
Because remember, characters are part of the ASCII table.
So you're going to create two files, a temporary file and then the file you
want to read that you already exist.
You have to create a data file that just contains some data in it.
And you want to close both of those and then rename and remove at the end.
So you're going to get pretty familiar with using fgetc.
And you'll also have to use fputc to write data to your temporary file
whenever you convert the string.
So you're going to convert the character and then the conversion
of the character you're then going to put do and fputc on that file.
So I hope this is going to be helpful.
If you have any questions, you can always email me.
Last thing you want to do as part of the program is display the contents
of the original file to standard output, so you're going to read again.
I don't want to have to open up the file to test.
You could read the data, look for the lower case and then
convert to uppercase and then open up the file manually.
But I don't want to look at the file.
So I want you to print it out, so you're going to read, write and
then read again as the last output.
But the end result again you only have one file created.
The temporary file is just going to create the temporary file then you're
going to rename to the old file.
So I'll provide a demonstration of how this is done.
There's many different ways you can do it, but we'll
try to do it the right way. Thank you.