Thread: replacing scanf with fgets

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    34

    replacing scanf with fgets

    float value;
    Code:
    scanf("%f", &libry[count++].value);
    I know I can replace the scanf() with atoi and fgets but how big should the buffer be? I'm not too sure how fgets works so I have a few questions concerning fgets.

    Code:
    fgets(str, 40, stdin)
    The above line of code would read in 40 - 1 characters correct? If so what is stored in str[39]?null terminator?

    If all 39 characters are read than does the newline stay in the input buffer?
    ~flood

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I know I can replace the scanf() with atoi and fgets
    don't know about that... might wanna look into how atoi works again. it doesn't replace scanf.

    response to how fgets works
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Big Edit: I originally wrote this lengthy reply answering all of the questions, but I'd instead like to encourage the original poster to create a simple test program (10 lines is all it takes) that answers the questions for him.
    Last edited by itsme86; 08-18-2004 at 07:22 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    thanks itsme86!!

    I have one problem though, how can I make sure that anything after 39 characters doesn't go to the input buffer? For example, the gets function would discard the newline character.


    edit: too late I already read your post LOL
    ~flood

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A simple way to clear everything after the fgets() up through the newline character would be to do something like:
    Code:
    while(getchar() != '\n')
      ;
    ...after the fgets(). Of course, you'll want to make sure the newline didn't get sucked out of the buffer during the fgets() call first

    EDIT: Well, use the test program advice for future reference then.
    Last edited by itsme86; 08-18-2004 at 07:30 PM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    again thanks man.

    I use the strchr() function to find newline characters and replace them with the null terminator.
    ~flood

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could have always just read the FAQ entry.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  2. is scanf as safe as fgets when used to receive string?
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 05:18 PM
  3. replacing scanf...
    By 8ball in forum C Programming
    Replies: 8
    Last Post: 05-15-2004, 08:45 PM
  4. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM
  5. String manipulation and scanf vs fgets
    By Nit in forum C Programming
    Replies: 9
    Last Post: 03-20-2002, 12:44 PM