Only partially printing a string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Only partially printing a string

 
0
  #1
Jan 14th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Only partially printing a string

 
0
  #2
Jan 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Only partially printing a string

 
0
  #3
Jan 14th, 2009
use fgets() -- never ever gets() -- to read the entire line from a file or from the keyboard.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Only partially printing a string

 
0
  #4
Jan 14th, 2009
If I put in fgets instead, then I get this error message:

line 20: error C2660: 'fgets' : function does not take 1 arguments
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Only partially printing a string

 
0
  #5
Jan 14th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Only partially printing a string

 
0
  #6
Jan 15th, 2009
You guys keep telling me to look in the docs. Well, if I understood the docs, then I wouldn't be here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Only partially printing a string

 
0
  #7
Jan 15th, 2009
Originally Posted by RenFromPenn View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Only partially printing a string

 
0
  #8
Jan 16th, 2009
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: Only partially printing a string

 
0
  #9
Jan 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: Only partially printing a string

 
0
  #10
Jan 21st, 2009
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..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC