Hi,
I am working on a program that opens a file, reads the text, converts the text to uppercase, and then displays it on the screen. If I write the program to just type in a string and convert it, it works fine. If, however, I write it to open and read a fine it doesn't work. In my test file I typed, "This is a test," but the program only prints "THIS." Can you please help me figure out why the rest of the sting isn't being printed?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
char str[300];
int i;
FILE *inFile;
char fileName[25];
//printf("Enter a string: ");
//gets(str);
printf("\nEnter a file name: ");
gets(fileName);
inFile = fopen(fileName, "r"); /*This opens the file */
if (inFile == NULL) /*Checks that the file exists*/
{
printf("\nThe file %s was not opened successfully.", fileName);
printf("\nPlease check that you entered the file name correctly.\n");
exit(1);
}
while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
fclose(inFile);
for( i = 0; str[ i ]; i++)
str[ i ] = toupper( str[ i ] );
printf("%s\n", str); /* uppercase string */
return 0;
}