can someone explain to me what is fgets()

This is a discussion on can someone explain to me what is fgets() within the C Programming forums, part of the General Programming Boards category; if we compile and execute this program Code: #include <stdio.h> int main ( void ) { char buffer[BUFSIZ]; printf ( ...

  1. #1
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70

    can someone explain to me what is fgets()

    if we compile and execute this program

    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char buffer[BUFSIZ];
    
      printf ( "Enter a string: " );
      if ( fgets ( buffer, sizeof buffer, stdin ) != NULL )
        printf ( "|%s|\n", buffer );
    
      return 0;
    }
    the output will be :
    Code:
    |test
    |
    can i know why ?!!
    because in the code there is no newline char btw. %s and the |

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I won't explain what fgets is; a reference can do that better.

    Your problem is that you write a line of input and hit enter, which inserts a newline into the stream. fgets() reads that newline and places it in the buffer. Thus, when you print the buffer, you get the newline.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks .

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    avoid that by
    Code:
    #include <stdio.h>
    
    int main ()
    {
      char buffer[BUFSIZ];
      char *p;
      
      printf ( "Enter a string: " );
      if ( fgets ( buffer, sizeof buffer, stdin ) != NULL )
      {
        p = strchr(buffer,'\n');
        *p='\0';    
        printf ( "|%s|\n", buffer );
      }
      
      getchar();
      return 0;
    }
    /* my output
    Enter a string: This is a test
    |This is a test|
    */
    ssharish2005

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Except that you need to test p for non-NULL, otherwise you get an access violation if the string does not contain a '\n' (usually because the line is longer than the buffer).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by CornedBee
    Except that you need to test p for non-NULL, otherwise you get an access violation if the string does not contain a '\n' (usually because the line is longer than the buffer).
    Code:
    #include <stdio.h>
    
    int main ()
    {
      char buffer[BUFSIZ];
      char *p;
      
      printf ( "Enter a string: " );
      if ( fgets ( buffer, sizeof buffer, stdin ) != NULL )
      {
        p = strchr(buffer,'\n');
        if(p != NULL)
              *p='\0';    
        printf ( "|%s|\n", buffer );
      }
      
      getchar();
      return 0;
    }
    /* my output
    Enter a string: This is a test
    |This is a test|
    */
    code updated

    ssharish2005

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, 05: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, 02: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

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