c - How to convert text file characters to uppercase letters? -


i using c programming language. want convert characters in text file uppercase letters , print text file contents on screen after conversion.

here code:

 #include <stdio.h>  #include <string.h>   int main()  {  file *fptr;  char filename[30];  char ch;  printf("enter filename want make capital letters: \n"); scanf("%s",filename);  fptr = fopen(filename,"r");  ch = fgetc(fptr); while(ch != eof) { ch = toupper(ch); printf("%c",ch); ch = fgetc(fptr); }  fclose(fptr);  return 0; } 

you on way! here recommended adjustments:

first, char ch should int ch. take @ manual page fgetch , find returns int. pretty important, because eof returned out of range char in range int.

next, simplify bit. why not:

while((ch = fgetc(ptr)) != eof) {   printf("%c", (char)ch); } 

this moves logical test , read single line, eliminating 2 lines have fgetch in them.

finally, should #include<ctype.h> since header file int toupper(int) defined.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -