the code below should convert the content of a file to uppercase (if conversion is necessary, it replaces the original character with the uppercase). The code is working on Mac OS and Linux. However on Windows, the conversion stops at the second letter and writes this second letter (uppercase) never ending in the file.
Example
Source data:
asdf
Result:
ASSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS...
I don't understand, why it's working on the other plattforms but for windows.
I'm very thankfull for any explanations.
regards, eljobso
#include <stdio.h>
#include <stdlib.h>
int to_upper_file(FILE *fp)
{
char ch = ' ';
if (fp == NULL)
{
perror("Unable to open file");
exit(0);
}
else
{
while (ch != EOF)
{
ch = fgetc(fp);
if ((ch >= 'a') && (ch <= 'z'))
{
ch = ch - 32;
fseek(fp, -1, SEEK_CUR);
fputc(ch, fp);
}
}
}
fclose(fp);
return 0;
}
int main(void)
{
FILE *fp;
int status;
char filename[20];
printf("Enter filename:");
scanf("%s", filename);
fp = fopen(filename, "r+");
if(!fp)
{
printf("File error.\n");
exit(0);
}
else
{
status = to_upper_file(fp);
if (status == 0)
{
printf("Conversion success.\n");
}
if (status == -1)
{
printf("Conversion failure.\n");
}
}
return 0;
}