943,608 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1020
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 14th, 2009
0

Only partially printing a string

Expand Post »
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?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. int main(void)
  6. {
  7.  
  8. char str[300];
  9. int i;
  10. FILE *inFile;
  11. char fileName[25];
  12.  
  13. //printf("Enter a string: ");
  14. //gets(str);
  15.  
  16. printf("\nEnter a file name: ");
  17. gets(fileName);
  18. inFile = fopen(fileName, "r"); /*This opens the file */
  19. if (inFile == NULL) /*Checks that the file exists*/
  20.  
  21. {
  22. printf("\nThe file %s was not opened successfully.", fileName);
  23. printf("\nPlease check that you entered the file name correctly.\n");
  24. exit(1);
  25. }
  26. while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
  27. fclose(inFile);
  28.  
  29. for( i = 0; str[ i ]; i++)
  30. str[ i ] = toupper( str[ i ] );
  31.  
  32. printf("%s\n", str); /* uppercase string */
  33.  
  34. return 0;
  35. }
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 14th, 2009
0

Re: Only partially printing a string

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.
Last edited by Aia; Jan 14th, 2009 at 6:58 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 14th, 2009
0

Re: Only partially printing a string

use fgets() -- never ever gets() -- to read the entire line from a file or from the keyboard.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jan 14th, 2009
0

Re: Only partially printing a string

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

line 20: error C2660: 'fgets' : function does not take 1 arguments
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 14th, 2009
0

Re: Only partially printing a string

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jan 15th, 2009
0

Re: Only partially printing a string

You guys keep telling me to look in the docs. Well, if I understood the docs, then I wouldn't be here.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 15th, 2009
0

Re: Only partially printing a string

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.
Last edited by Aia; Jan 15th, 2009 at 6:28 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 16th, 2009
0

Re: Only partially printing a string

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jan 21st, 2009
0

Re: Only partially printing a string

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

        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
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009
Jan 21st, 2009
0

Re: Only partially printing a string

Here is the prototype solution...
  1. while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
  2. {
  3. for( i = 0; str[ i ]; i++)
  4. str[ i ] = toupper( str[ i ] );
  5.  
  6. printf("%s\n", str); /* uppercase string */
  7. }
  8. fclose(inFile);

Hope this helps..
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: reading an integer from file in c
Next Thread in C Forum Timeline: How do I calculate the closest number to a number?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC