clearing buffer after reading string w/ scanf()

This is a discussion on clearing buffer after reading string w/ scanf() within the C Programming forums, part of the General Programming Boards category; Why do I have to clear the input buffer after reading a string with scanf()? Why do I have to ...

  1. #1
    novice fisheromen1031's Avatar
    Join Date
    Jul 2005
    Location
    Lone Star State/ Rocket City
    Posts
    13

    clearing buffer after reading string w/ scanf()

    Why do I have to clear the input buffer after reading a string with scanf()?
    Why do I have to do this to keep may console screen from disappearing:
    Code:
    #include <stdio.h>
    
    int main()
    {
    char string_ch[50];
    printf("please enter a string: ");
    scanf("%s", string_ch);
    while (getchar() != '\n');
    printf("please press enter");
    getchar();
    return 0;
    }
    instead of this?
    Code:
    #include <stdio.h>
    
    int main()
    {
    char string_ch[50];
    printf("please enter a string: ");
    scanf("%s", string_ch);
    printf("please press enter");
    getchar();
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Because scanf() with %s doesn't read a newline, so it's just left there for the next input function to deal with.
    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.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Yes, getchar() is taking the left over '\n' that scanf doesn't use.
    And reading %s with scanf is vulnerable to buffer overflows. You shoul use
    Code:
    int main()
    {
      char string_ch[50];
      printf("please enter a string: ");
      fgets(string_ch,sizeof(string_ch),stdin);
      printf("please press enter");
      getchar();
      return 0;
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And you may need to get rid of the '\n' on the end of the string that fgets() adds on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    fgets doesn't add a '\n'. it simply leaves it. That way you know if you got the entire file (one of the shortcomings of it fgets)

  6. #6
    novice fisheromen1031's Avatar
    Join Date
    Jul 2005
    Location
    Lone Star State/ Rocket City
    Posts
    13
    sounds like one has to clear the input buffer manually for both fgets and scanf

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > fgets doesn't add a '\n'. it simply leaves it.
    It only leaves it if there isn't room in the buffer you provide.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    it leaves it if there isn't room? What?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Try it - nice simple program with a buffer size of say 10 bytes, then type in a nice long line and see how many fgets calls you have to make before one of them has a newline in it.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Yes i'm aware of how fgets works, your statement did not make sense though. How would it have a '\n' if there is no room for it?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Code:
    #include <stdio.h>
    #include <string.h>
    int main ( void ) {
      char buff[10];
      printf("Type in some long lines\n");
      while ( fgets(buff,sizeof buff,stdin) != NULL ) {
        int hasNewline = strchr(buff,'\n') != NULL;
        printf( "%d %d %s\n", strlen(buff), hasNewline, buff );
      }
      return 0;
    }
    
    
    ./a.out
    Type in some long lines
    hello world, how are you today
    9 0 hello wor
    9 0 ld, how a
    9 0 re you to
    4 1 day
    See - if the buffer fills before the \n, then there is no newline appended (but there's always a \0)
    If there is a newline, and it fits in the buffer, then fgets() returns at that point as well.
    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.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I think there's just a miscommunication going on. One person is talking about leaving the newline in the buffer passed to fgets() (instead of "adding" it there) and another person is talking about leaving it in the internal input buffer.

    Just to make sure it's clear, scanf() leaves the newline in the internal input buffer. fgets() grabs the newline (if there's room) and places it in the buffer you pass to it. If the buffer you pass to fgets() is too small to accomodate the entire line, it will only store as much as it can and leave the rest (including the newline) in the internal input buffer. fgets() will never add a newline where it didn't already exist.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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