Thread: fgets

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    fgets

    Hi all,

    I am trying to use fgets to read input from user. The funtion works fine enough, but according to the spec. fgets should only read until number of char are read according to length sent (or newline or eof). So my understanding is if i do the following:

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main(void){
       char buffer[100];
       while(1){
    	   fgets(buffer, 5, stdin); // read 5 char
    	   printf(buffer); // print buffer to test 
       }
    }
    This should only read 5 characters from keybord and then print the buffer? But it dosnt. The fgets function keeps on going until i press enter. Am i missing something here?

    G'n'R

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    the enter tells the keyboard to clear the buffer, then fgets will read 5 chars of the flushed buffer. it sounds you wants something like get5ch() or something.
    // edit, that is not a real function.

  3. #3
    root
    Join Date
    Sep 2003
    Posts
    232
    >This should only read 5 characters from keybord and then print the buffer?
    Since fgets specifies that n-1 characters are read where n is the second argument, your code *should* only print 4 characters and the 5th is set to '\0'.

    >But it dosnt. The fgets function keeps on going until i press enter.
    Your implementation of fgets is broken then. The following code does what you would expect (bolded text is user input):
    Code:
    user: /home/user/junk
    % cat test.c
    #include <stdio.h>
    
    int main ( ) {
      char buffer[100];
    
      if (fgets(buffer, 5, stdin) != NULL)
        printf("|%s|\n", buffer);
    
      return 0;
    }
    
    user: /home/user/junk
    % gcc -W -Wall -ansi -pedantic test.c; ./a.out
    mookie mookie
    |mook|
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.
    From: http://www.rt.com/man/fgets.3.html

    To do what you want G'n'R you are going to need to put a loop using getche() (need conio.h). See http://faq.cprogramming.com/cgi-bin/...&id=1043284392 for more info.
    Code:
    int main (void)
    {
    int count=0;
    char buffer[11];
    
    while ( count < 10 )
      {
      ch = getche();
      if ( ch == '\n' )
         break;
      buffer[count]=ch;
      count  += 1;
      }
    buffer[count] = '\0';
    }
    disclaimer: code is untested, but can serve as a guide.

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >To do what you want G'n'R you are going to need to put a loop using getche() (need conio.h).
    I don't see why this is needed. From my interpretation of the program, the output should be something like this:
    Code:
    % cat test.c
    #include <stdio.h>
    
    int main ( ) {
      char buffer[100];
    
      while (fgets(buffer, 5, stdin) != NULL)
        printf("|%s|\n", buffer);
    
      return 0;
    }
    
    user: /home/user/junk
    % gcc test.c; ./a.out
    mookie mookie
    |mook|
    |ie m|
    |ooki|
    |e
    |
    ^D
    fgets is defined to stop reading if one of four things happen: 1) A newline is encountered. 2) EOF is encountered. 3) N-1 characters have been read (eg. the buffer is filled). 4) A read error occurs. Since G'n'R's fgets impelentation doesn't do all of this properly, we can then try to work out alternative solutions. However, there's no need to step outside the bounds of ANSI C to do it.
    Code:
    user: /home/user/junk
    % cat test.c
    #include <stdio.h>
    
    char *reads(char *b, int lim, FILE *fd) {
      int c;
      int i;
    
      i = 0;
      while (i < lim - 1) {
        if ((c = getc(fd)) == EOF)
          break;
        b[i++] = c;
        if (c == '\n')
          break;
      }
      b[i] = '\0';
      if (ferror(fd) || feof(fd))
        return NULL;
    
      return b;
    }
    
    int main ( ) {
      char buffer[100];
    
      while (reads(buffer, 5, stdin) != NULL)
        printf("|%s|\n", buffer);
    
      return 0;
    }
    
    user: /home/user/junk
    % gcc test.c; ./a.out
    mookie mookie
    |mook|
    |ie m|
    |ooki|
    |e
    |
    ^D
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Read what he is saying:
    This should only read 5 characters from keybord and then print the buffer? But it dosnt. The fgets function keeps on going until i press enter.
    It seems he wants the program to read a certain number of characters then print the buffer immediately without the user having to press enter.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    fgetsssssssssssssss

    Yo,

    Thanks for the replies. I have to look at this later. My brain is dead by the socket program i am writing. However, seems i have misunderstood fgets. I though the function internally counted the number of chars you wanted, filling the buffer you send to it at the same time. After it reaches the count you specify it simply returned (or one of the other conditions)

    I'll check it out. If this is not so, yes, write a function to do it for you is the go. e.g. the getch example...

    Cheers,
    G'n'R

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    fgets() doesn't do any *live* checking of the input. All it does is takes what the user inputed, looks at the first x characters (well actually x - 1), and puts them into a string, and slaps a null character at the end.

    Hope that helps.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Hi,

    So it dosnt return before you hit enter. However, buffer sent is only filled with us much char as specified in count (if count specified. Cool, in that tw post of the use of fgets is excatly what you would get.....You can type for ever, but when you hit enter, buffer is filled only with the abount of chars your specify.
    Ok, getche it is then...


    >>tw
    Code:
    % cat test.c
    #include <stdio.h>
    
    int main ( ) {
      char buffer[100];
    
      while (fgets(buffer, 5, stdin) != NULL)
        printf("|%s|\n", buffer);
    
      return 0;
    }
    
    user: /home/user/junk
    % gcc test.c; ./a.out
    mookie mookie
    |mook|
    |ie m|
    |ooki|
    |e
    |
    G'n'R

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM