Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi everyone! im looking for a little help for an assignment. im using the fgets() function to stored a string in an array of size 20...it works fine, however i ...
  1. #1
    Just Joined! Enforcer's Avatar
    Join Date
    Sep 2007
    Posts
    10

    fgets problem with while loop

    Hi everyone!

    im looking for a little help for an assignment.

    im using the fgets() function to stored a string in an array of size 20...it works fine, however i need it to continually ask for input, and so i used a while loop.

    the problem here is that if an input greater than 20 characters in entered then fgets will store the first 20 and continue on, but when it loops back around it will clear the array and insert the excess characters and continue until it runs out of input characters.

    while (trigger != 0)
    {
    if (strlen(fgets(isbnInput, 20, stdin)) != 1 )
    {
    /* ive removed excess code here because its irrelevant */
    }
    else
    {
    trigger = 0;
    }
    }

    any help would be greatly appreciated thanks!!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Righto.

    The problem here is that stdin is actually a stream, and more specifically, a buffered stream. So when you run fgets() on any buffered file stream, what actually happens is:

    1. fgets() checks to see if there is currently any data in the file stream's buffer.
    2a. If there is data in the buffer, fgets() returns the first 19 characters of it (or less if there are less than 19 characters).
    2b. If there is no data in the buffer, more data is requested from the stream. In the case of a file, more of the file is read. In the case of stdin, the user is prompted for more input. fgets() then goes to step 2a.

    So if you want to force a brand new set of data on each read, you need to clear all of the data in the buffer. This is called "flushing", and there is a function called fflush(FILE *) that will flush any output buffer.

    Unfortunately, in this case, we are looking at an input buffer. There is no function in the standard C Library for this, and I do not believe that there is one in glibc. I am not sure of the best way to write such a function, but hopefully you will learn a bit about how these streams work, and can start looking for a solution on your own.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined! Enforcer's Avatar
    Join Date
    Sep 2007
    Posts
    10
    oh wow...ic ic
    thanks alot for replying so quickly

    ill get working on that right away...cheers...appreciate it...

    I also found a website that may help to accomplish this so ill leave it here for any others that need it :P

    Common C Programming Errors

    thanks again

  4. #4
    Just Joined! Enforcer's Avatar
    Join Date
    Sep 2007
    Posts
    10
    and another clearer one...same coding...just easier to find on the page since its specifically made to demo the clearing of an input buffer

    Cprogramming.com: FAQ

  5. #5
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    If I understand what you're doing, normally I store what I get from fgets in a buffer, then use sscanf to get whatever I need out of it. So I do like
    Code:
    while (fgets(buffer, sizeof(buffer), stdin) != EOF) {
       do whatever to buffer
    }

    AFAIK you don't even really even need to put != EOF, but I do it for clarity. Once you have that buffer read, you can do strlen on it, or strcmp, whatever you want.

  6. #6
    Just Joined! Enforcer's Avatar
    Join Date
    Sep 2007
    Posts
    10
    thanks for that

    unfortunately that assignmnet specs reads that i need to erase unread characters i.e. characters that exceeed the 20 char array size (i assume) so i guess clearing the input buffer is the way to go here...thx

  7. #7
    Just Joined! Enforcer's Avatar
    Join Date
    Sep 2007
    Posts
    10
    ok...just got a solution for this...
    basically the way to clear the buffer is to read from it...

    so...

    Code:
    char temp;
    
    do scanf("%c", &temp);
    while (temp != '\n');
    that should work...seems to be working for me anyway

  8. #8
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Sorry forgot about this thread... though you are not posting the whole problem, you might want to look into fflush() if it fits for this scenario. I find myself using it in situations with curses and getch() on Solaris.

    -- EDIT --
    Oops didn't notice that's already been mentioned. Either way, you've found your solution

  9. #9
    Just Joined! Enforcer's Avatar
    Join Date
    Sep 2007
    Posts
    10
    Actually fflush(stdin) worked...but compiling it at uni produced alot of errors because its 'overhead expensive' as someone said...so yeah just an alternative solution

  10. #10
    Just Joined!
    Join Date
    Sep 2007
    Posts
    1
    I have got the same problem as yours. When fflush is used on the local machine, everything is working....no error message, on warrring message. However, when I submit it, the testing program will be stopped.....

    if you can find something to fix this problem, please post it here...

    cheers!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...