C Program to convert contents of a file to upper case
Program
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int file_to_upper(FILE *);
void main()
{
FILE *fp;
int status;
char file_name[30];
printf("Enter the file name you would like to convert:\n");
scanf(" %s", file_name);
fp = fopen(file_name, "r+");
status = file_to_upper(fp);
if(status == 1)
printf("File %s converted to upper case successfully\n", file_name);
else
printf("Unable to convert.");
}
int file_to_upper(FILE *fp)
{
char ch;
if (fp == NULL)
{
puts("Error!!! Cannot open file");
return -1;
}
do
{
ch = fgetc(fp);
if ((ch >= 'a') && (ch <= 'z'))
{
ch = toupper(ch);
fseek(fp, -1, SEEK_CUR);
fputc(ch, fp);
}
}
while (ch != EOF);
fclose(fp);
return 1;
}
Output
Enter the file name you would like to convert:
source.txt
File source.txt converted to upper case successfully