Like Tree2Likes
  • 2 Post By Salem

Program closes when buffer is over limit fgets

This is a discussion on Program closes when buffer is over limit fgets within the C Programming forums, part of the General Programming Boards category; Hey, I'm using the following code: Buffer size is 20 bytes fgets(buffer,INPUT_BUFFER,stdin); when the user inputs 20 or less characters ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Program closes when buffer is over limit fgets

    Hey,

    I'm using the following code: Buffer size is 20 bytes
    fgets(buffer,INPUT_BUFFER,stdin);

    when the user inputs 20 or less characters into the console, the program runs fine.
    But when the user enters the amount of characters above the buffer into the console the program automatically closes?

    How can I stop this?
    Last edited by Salem; 08-19-2011 at 10:04 AM. Reason: Restored original

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Don't ask vaporous questions about situations we can't possibly know the first thing about...

    Post your code!
    Ask a specific question.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Presumably you have some later bit of code for "press any key to exit" kind of thing.

    See the FAQ for some ideas on how to "flush" the input stream.
    Personally, I would recommend a BUFSIZ buffer rather than 20.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Ty salem, +rep to you

    I needed to flush input stream
    Last edited by NativeCodeX; 08-19-2011 at 07:59 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NativeCodeX View Post
    Here is the code:
    Code:
    printf("Enter Name -> ");
    char *buffer=(char *)malloc(INPUT_BUFFER);
    	memset(buffer,'\0',sizeof(buffer));
    	fgets(buffer,INPUT_BUFFER,stdin);
    	if(buffer[strlen(buffer)-1]=='\n'){
    		buffer[strlen(buffer)-1]='\0';
    	}
    	getchar();
    //The getchar only pauses the program when the characters entered into the console are less than the buffer
    Of course getchar() only pauses when the buffer is less than full. When the buffer is filled fgets() returns with stuff still in the input buffer and getchar() will read the next character in the chain and carry on like it should.

    Two suggestions...
    Make a bigger buffer ... 20 bytes is ludicrously small... try using the BUFSIZ constant, which I believe is 512 on most systems.
    Clear the input buffer the instant fgets() returns... while(getchar() != \n); .... should do the job.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by NativeCodeX View Post
    Resolved
    Nice job, you just made all the comments the posters made completely pointless. DO NOT EDIT OUT YOUR ORIGINAL QUESTION. It invalidates the work people here did to help you and prevents the community from learning from your mistakes. This is an all volunteer service here and thus the whole point is for people to learn from eachother. By removing the original question, you are preventing that from happening.

    Way to help out the community. I for one will make sure I never help you now.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Nice job, you just made all the comments the posters made completely pointless. DO NOT EDIT OUT YOUR ORIGINAL QUESTION. It invalidates the work people here did to help you and prevents the community from learning from your mistakes. This is an all volunteer service here and thus the whole point is for people to learn from eachother. By removing the original question, you are preventing that from happening.

    Way to help out the community. I for one will make sure I never help you now.
    Probably didn't want his teacher to see that he got help with his homework...

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Probably didn't want his teacher to see that he got help with his homework...
    Most likely, however that is not really my concern. We have a pretty decent hw policy here that is enforced rather well and from what I saw from the responses wasn't violated at all. If he is not allowed to even ask for pointers than he shouldn't have posted. I view this as completely rude and selfish behavior and I for one will make it a point to not help the OP out in the future because of this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I wonder if our friend Webmaster could make it so people can't edit message 1, in a thread?

    Or maybe he could set it up so that people with fewer than a dozen or so messages have no editing priveledge at all.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by CommonTater View Post
    I wonder if our friend Webmaster could make it so people can't edit message 1, in a thread?

    Or maybe he could set it up so that people with fewer than a dozen or so messages have no editing priveledge at all.
    The later is a nice idea.
    I'd suggest another idea. No Editing for anyone, just append ! (Most of us append anyway...instead of editing.)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    The later is a nice idea.
    I'd suggest another idea. No Editing for anyone, just append ! (Most of us append anyway...instead of editing.)
    And how do we deal with things like bad formatting in code tags, magic smilies, spelling errors and typos...

    If I had a vote on this, I'd say "No editing for the first 25 messages"... A member should know the board rules by then.

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    And how do we deal with things like bad formatting in code tags, magic smilies, spelling errors and typos...
    Oh..forgot about those..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Oh..forgot about those..
    And I'm betting we all wish we could

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Restored, fixed and closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Excess buffer using fgets
    By $l4xklynx in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 03:56 AM
  2. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:40 AM
  3. fgets buffer
    By Pythonsnake in forum C Programming
    Replies: 8
    Last Post: 01-10-2006, 11:27 PM
  4. Confused about fgets, clearing the buffer, etc
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 11:03 AM
  5. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 09:41 AM

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