fgets, fflush, strcpy...

This is a discussion on fgets, fflush, strcpy... within the C Programming forums, part of the General Programming Boards category; Hi, Can anyone tell me if they see any glaring mistakes in this piece of code? Code: void vGetString(char szString[], ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    fgets, fflush, strcpy...

    Hi,

    Can anyone tell me if they see any glaring mistakes in this piece of code?

    Code:
    void vGetString(char szString[], int iMax)
    {
    	char szTmpBuf[STRING_BUF];                  // set temp buffer size 
    	
            while (getchar() != '\n');                        
    	fgets(szTmpBuf,sizeof(szTmpBuf),stdin);     // get string from keyboard
    	
    
    	if sizeof(szTmpBuf) > (iMax - 1))
    	{
    		szTmpBuf[iMax - 1] = '\0';                // truncate
    	}
    	strcpy(szString,szTmpBuf);                       // save buffer	
    	fflush(stdin);                                           // flush keyboard
    }
    This code makes my next function always return FALSE.

    here is my prototype:

    Code:
    void vGetString(char szString[], int iMax);
    And I call it like this:

    Code:
            char szTitle[MAX_ITEMS][MAX_TITLE + 1];
            
            printf ("    title                       : ");
            vGetString(szTitle[iLimit], MAX_TITLE);
    	printf ("    author                      : ");
    	vGetString(szAuthor[iLimit], MAX_AUTHOR);
    	printf ("    publisher                   : ");
    	vGetString(szPub[iLimit], MAX_PUB);
    	printf ("    category 1 (F or N)         : ");
    	cCat1[iLimit] = cGet1of2 ('F', 'N');

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > Explanations of... > Why fflush(stdin) is wrong
    FAQ > Prelude's Corner > Pointers And Arrays (intermediate)
    --The last one is in regard to your use of sizeof(szTmpBuf); it's down there a ways under "Rule 3".
    Last edited by Dave_Sinkula; 05-11-2004 at 01:59 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've also got some potential problems with your fgets call in relation to the buffer you're passing it. You should try to have your buffers use the same magic number:
    Code:
    char szTitle[MAX_ITEMS][MAX_TITLE + 1];
    ...
    void vGetString(char szString[], int iMax)
    {
    	char szTmpBuf[STRING_BUF];                  // set temp buffer size
    You'd better hope that STRING_BUF is always smaller than MAX_TITLE+ 1. For instance, were you to later decide you need a bigger string buffer, you may now be blowing past the end of your buffer. Consistancy helps.

    Code:
    fgets(szTmpBuf,sizeof(szTmpBuf),stdin);     // get string from keyboard
    	
    
    if sizeof(szTmpBuf) > (iMax - 1))
    You're missing a left parenthesis on your if statement, but I'll assume that's a typo. In regards to what Dave said, you are actually fine in using sizeof(szTmpBuf) because szTmpBuf is created in scope. The problem would be if you were trying to do:
    Code:
    void vGetString(char szString[], int iMax)
    {
        if( sizeof( szString ) ...
    Which is what I think Dave was thinking of. However, your use of sizeof as you've used it is correct, because szTmpBuf was created in the scope you use the sizeof operator.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by quzah
    In regards to what Dave said, you are actually fine in using sizeof(szTmpBuf) because szTmpBuf is created in scope.
    Yup. My bad. Thanks for the correction. (I think I get thrown off by the Hungarian notation warts.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    Hi,

    Thanks for the infomation. I think I have enough to solve the problem. I'll report back.

    Matt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 09:10 AM
  3. When and where to use fflush (stdout)?
    By Micko in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 11:58 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 01:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21