Thread: scanf( "%s", &array )

  1. #16
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by C_ntua View Post
    Why? fgets() will read 1023 characters, put a null character at the end and that's it. A line is terminated by a \n character, so you can just check if the 1023 element of line is \n. If it is then it means you read a whole line. If not then you didn't. So you can just fgets() again.
    First, I consider calling fgets() again to continue reading a line to be an annoyance.

    You shouldn't just blindly check the 1023rd element, of course: it might not have been written to at all. You'd check whether a newline exists in the string. Read again if necessary (how many times? Do you do this recursively each time a read happens without a newline?) And, of course, the last line of input might not contain a newline. I believe C89 allows for EOF to not be sticky, so you might be able to add in that little complication as well.

    With all of this put together, I'm of the opinion that fgets() can be annoying. Non-standard functions like GNU's getline() (which is a fleshed-out version of your other examples) do make it pretty painless to read a line, but that doesn't change my opinion of fgets(). There's a trade-off, too (Should a line-reading function just keep blindly reading and resizing, potentially chewing up all available memory?) but I'm quite comfortable being annoyed with all solutions to a problem.

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using realloc on each character is overkill

    %30c will not read several characters - only one

    %s reads till the first whitespace - why all this fuss about reading till the new-line character? It is not what OP is asking
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Thanks all.

    After going through your replys, and relising i could have just used gets().
    I've decided to go with this combo:
    Code:
    . include <stdio.h>
    
    . define   MAX   5
    
    int main()
    {
      char     name[MAX];
      int      num     =     0;
      int      ch;
    
      printf("\n\n ENTER YOUR NAME: ");
      ch    =    getc( stdin );
    
      while( ( ch != '\n' ) && ( num < MAX ) )   // maybe add alittle [ && ( ch != ' ' ) ] to the condition.
      {
        name[num]     =    (char) ch;
        num++;
        ch            =     getc( stdin );
      }
    
      name[num]       =     '\0';
      printf("\n\n &#37;s \n\n", name );
    
      return 0;
    };
    Just as a tester... Can anybody see any problems with that other than the loss of input over the defined maximum?
    Which leads me to my next question:
    How do i clear the buffer...?

    ( ps: My <hash> key is broken. )
    Last edited by Else; 09-15-2008 at 11:17 AM.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    num < MAX

    if you exit loop due to this condition - num will be == MAX

    so
    name[num] = '\0';
    will be out of bounds access
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >( ps: My <hash> key is broken. )

    Enable trigraphs and use ??= instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM