cpp
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char x;
FILE *pUpper;
FILE *upper;
pUpper = fopen("gettys.txt", "r+");
upper = fopen("upper.txt", "w+");
if (!pUpper) // This is just personal preference, you code was not incorrect
printf("\nFile gettys.txt cannot be opened\n");
if (!upper)
printf("\nFile upper.txt cannnot be opened\n");
while ( !feof(pUpper) ) {
fscanf(pUpper, "%c", &x);
if (isalpha(x) && islower(x)) {
x=toupper(x);
}
fprintf(upper, "%c", x);
}
printf("\nFinished writing file upper.txt.\n");
fclose(pUpper);
fclose(upper);
}
Basically, I only altered a few things, as you were just about there.
1.)
if (!pUpper) This is just personal preference, you code was not incorrect
2.) I changed
int x to type char. Since you were reading directly to x, every character, regardless of it's type. I also removed the str array that you had, because you were reading & writing character x, & performing your tests on x. The fact that you built an array was irrelevant, because you never used it.
3.) Use fscanf to read on a per-character basis, rather than pull in one letter as a string. Also, changed it to read to the address with the ampersand.
4.) You ended your if statement before it did anything
if (isalpha(x) && islower(x)); & then you had an open bracket underneath. So the code would have never went into the if condition.
I hope this helps!
Almost forgot too show example output:
QUOTE
>$./upc
Finished writing file upper.txt.
>$cat upper.txt
FOUR SCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH ON THIS CONTINENT
A NEW NATION, CONCEIVED IN LIBERTY, AND DEDICATED TO THE PROPOSITION THAT ALL
MEN ARE CREATED EQUAL. NOW WE ARE ENGAGED IN A GREAT CIVIL WAR, TESTING