Raluca Damaris Lupeş Raluca Damaris Lupeş - 1 month ago 7
C Question

Writing in a file c

So basically in the program below I try to convert the upper case letter of a text into lower case letters and the lower case letters into upper case letter and print out the result in a file...but the only thing I get after running the program is a sequence of numbers... I tried to put as a second parameter to fprintf function "%s" but I didn't get a better result...

#include <stdio.h>
#include <ctype.h>

int main( )

{
char c;

int charToLowerCase = 0;
int charToUpperCase = 0;
int countCharacters = 0;
FILE *in_file;
FILE *out_file;
in_file = fopen("input.txt", "r");
out_file = fopen("output.txt", "w");
c = fgetc(in_file);
while (c != EOF)
{
if (c >= 'A' && c <= 'Z')
{
fprintf(out_file, "%d", tolower(c));
charToLowerCase++;
}
else if (c >= 'a' && c <= 'z')
{
fprintf(out_file, "%d", toupper(c));
charToUpperCase++;
}
else
fprintf(out_file, "%d", c);

c = fgetc(in_file);
countCharacters++;
}
fclose(in_file);
fclose(out_file);
return 0;
}

Answer

Use %c in your fprintf statements. %d is for integers.

http://www.cplusplus.com/reference/cstdio/fprintf/