Thread: can some please demonstrate fgets ?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    can some please demonstrate fgets ?

    I need to use it to read the file name from the user. I tried to use fgets but my program is terminating abnormally. How to do error handling for fgets ? Is it sufficient to check if the returned char pointer is null ?? Right now I'm using gets, but I've been told that its dangerous and my gcc compiler gives warning.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char fname[50];
      FILE *fp;
    
      printf("Enter file name\n");
      gets(fname);
    
      fp = fopen(fname, "r");
      if(fp  == NULL)
      {
        fprintf(stderr, "&#37;s[%d]  : ERROR WHILE OPENING THE FILE\n",__FILE__, __LINE__);
         return (-1);
      }
    
      return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    char *ptr = NULL;
    
    ....
    
    fgets(fname, sizeof(fname), stdin);
    if((ptr = strchr(fname, '\n')) != NULL)
    {
        *ptr = '\0';
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by MacGyver View Post
    Code:
    char *ptr = NULL;
    
    ....
    
    fgets(fname, sizeof(fname), stdin);
    if((ptr = strchr(fname, '\n')) != NULL)
    {
        *ptr = '\0';
    }
    what if fgets returns a NULL ??

    and is it ok to write a else case which will print an error message incase the buffer overflowed ?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by broli86 View Post
    what if fgets returns a NULL ??
    Then it failed. You probably want to quit at that point.

    Quote Originally Posted by broli86 View Post
    and is it ok to write a else case which will print an error message incase the buffer overflowed ?
    The buffer can't overflow. You're passing the size of the array, and fgets() knows not to overflow. This is why you're supposed to use fgets() over gets() in the first place!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    Then it failed. You probably want to quit at that point.
    And it's a good idea to check for that before the strrchr(), as I doubt that strrchr() is "NULL-safe".


    The buffer can't overflow. You're passing the size of the array, and fgets() knows not to overflow. This is why you're supposed to use fgets() over gets() in the first place!
    I think broli is referring to when the input is not complete (that is, there is no '\n' at the end of the string - in which case the input was longer than expected - this PROBABLY needs to be dealt with - and as opposed to gets(), there is a possibility of fixing it here, because the application can check, rather than most likely just crashing!

    So, if there is no \n at the end of the input, then the application received a string that was too long. Clear the input, e.g.
    Code:
    void clear_input(FILE *in)
    {
        int ch;
        while((ch = fgets(in)) != '\n' && ch != EOF) ;
    }
    Then ask again (if the input is console, if the input is a file, then you probably just want to print an informative message and quit - that is, if you can tell the difference).

    --
    Mats
    Last edited by matsp; 06-26-2008 at 02:43 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by matsp View Post
    And it's a good idea to check for that before the strrchr(), as I doubt that strrchr() is "NULL-safe".
    Agreed.

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