Member Avatar for RenFromPenn

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;
}

Recommended Answers

All 10 Replies

Read your reference material about how the standard fscanf() function works.
While your are there, read about gets() as well. If it is a good reference it would tell you to never use it.

use fgets() -- never ever gets() -- to read the entire line from a file or from the keyboard.

Member Avatar for RenFromPenn

If I put in fgets instead, then I get this error message:

line 20: error C2660: 'fgets' : function does not take 1 arguments

That's true -- it doesn't take one parameter. You need to look up the functions you want to use either in your compiler's printed docs (if they still have them) or on the net (ever hear of google?). Don't just toss functions on a *.cpp file and expect the program to compiler.

Member Avatar for RenFromPenn

You guys keep telling me to look in the docs. Well, if I understood the docs, then I wouldn't be here.

You guys keep telling me to look in the docs. Well, if I understood the docs, then I wouldn't be here.

Well, then the reasonable thing to do first would be to learn how to understand the documentation. Because you are not going to get very far by depending on others to tell you how you should program. Expecting that someone would be always willing to hold your hand is not going to work neither.

while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
        fclose(inFile);  //[B]Why are you closing the file in mid of everything[/B]

        for( i = 0; str[ i ]; i++)
    str[ i ] = toupper( str[ i ] );

  printf("%s\n", str); /* uppercase string */

        return 0;
}

if you close the file just after reading the first line, then its quite obvious that the buffer 'str' will contain only the first line from the file and the next call to fscanf will fail and hence the while loop will terminate after 1st call to fscanf and hence no further line from the file will be read..

You should close the file after all the processing is done...hope you understood

Here is the prototype solution...

while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
{
        for( i = 0; str[ i ]; i++)
        str[ i ] = toupper( str[ i ] );

        printf("%s\n", str); /* uppercase string */
}
fclose(inFile);

Hope this helps..

while the file is being read if in between it is closed then how it will be able to read and perform other functions.
I think you should use fread() function to read the files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.