PPS Lab · Experiment 29
Copy a file converting lowercase to uppercase
CFilesJNTUH R22 · CS108ES
Aim
To write a C program that copies one text file to another, replacing every lowercase letter with its uppercase equivalent. The source and destination file names are given as command-line arguments.
Algorithm
- Check that two arguments were given: the source file and the destination file. If not, print how to use the program and stop.
- Open the source file in read mode ("r"). If it cannot be opened, print an error and stop.
- Open the destination file in write mode ("w"). If it cannot be opened, close the source, print an error and stop.
- Read one character at a time from the source with
fgetcuntilEOF. - If the character is a lowercase letter, change it to uppercase with
toupper. Write the character to the destination withfputc. - Close both files and print a message.
Program
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <source file> <destination file>\n", argv[0]);
return 1;
}
FILE *src = fopen(argv[1], "r");
if (src == NULL) {
printf("Cannot open file %s\n", argv[1]);
return 1;
}
FILE *dest = fopen(argv[2], "w");
if (dest == NULL) {
printf("Cannot create file %s\n", argv[2]);
fclose(src);
return 1;
}
int ch;
while ((ch = fgetc(src)) != EOF) {
if (islower(ch)) {
ch = toupper(ch);
}
fputc(ch, dest);
}
fclose(src);
fclose(dest);
printf("Copied %s to %s in uppercase\n", argv[1], argv[2]);
return 0;
}Output
Programming for Problem Solving
C was developed by Dennis Ritchie in 1972.
file_handling.c uses fopen(), fgetc() and fputc().$ ./a.out input.txt output.txt
Copied input.txt to output.txt in uppercase
$ cat output.txt
PROGRAMMING FOR PROBLEM SOLVING
C WAS DEVELOPED BY DENNIS RITCHIE IN 1972.
FILE_HANDLING.C USES FOPEN(), FGETC() AND FPUTC().Recorded by compiling and running this exact program.
How it works
islower and toupper come from ctype.h. toupper already returns any character that is not a lowercase letter unchanged, so the islower check is not strictly needed, but it makes the program's logic clear.
Digits, spaces, punctuation and letters that are already uppercase are copied as they are.
Opening the destination with "w" creates the file if it does not exist and empties it if it does.
Common mistakes
Converting with
ch - 32without checking thatchis a lowercase letter. Every other character, like a space or a digit, is then changed too. Usetoupper, or checkch >= 'a' && ch <= 'z'first.Opening the destination file in
"r"mode. A file opened for reading cannot be written to; use"w".Forgetting
fclose(dest). Output is buffered, so some of the last characters may never reach the file.Giving the same file as source and destination. Opening it with
"w"empties it before anything is read, so all the data is lost.
Viva questions
Try to answer each one aloud before you open it.
What does toupper do, and which header declares it?
toupper(c)returns the uppercase form of c if c is a lowercase letter, and returns c unchanged otherwise. It is declared inctype.h.How are uppercase and lowercase letters related in ASCII?
'A' to 'Z' are 65 to 90 and 'a' to 'z' are 97 to 122. Each lowercase letter is 32 more than its uppercase letter.
What is the difference between fputc and putchar?
fputc(c, fp)writes a character to the filefp.putchar(c)writes it to the screen, the same asfputc(c, stdout).What happens if you open an existing file with mode "w"?
Its old contents are erased, and the file starts empty.
What is argv[0]?
The name used to run the program, for example
./a.out.