954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Only partially printing a string

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;
}
RenFromPenn
Light Poster
40 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

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

RenFromPenn
Light Poster
40 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

RenFromPenn
Light Poster
40 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 
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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
        fclose(inFile);  //<strong>Why are you closing the file in mid of everything</strong>

        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

me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

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..

me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

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.

ajay.krish123
Junior Poster in Training
90 posts since Nov 2008
Reputation Points: 6
Solved Threads: 9
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You