Thread: Usage of fgets

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Usage of fgets

    <<< split from fgets in while loop >>>

    I have the same problem now, how to solve this?
    Use another fgets to retrieve those "rubbish data" and ignore it? This way seems to be ugly.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fgets() is designed to take a whole row of text, into one string char.

    Are you trying to separate each word out of a line of text?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    No.
    The real problem is, I want to store user input into a char[], so I can allocate memory as char[10], I expect the user input data 10 bytes each row, and I will request the user input for 3 rows, however, what user input is not under my control, so if the user input 12 characters in the first line, then the extra 2 characters will be automatically read by the next fgets function call, this is not I expected.

    I want to take the first 10 bytes from user input, and ignore the rest of user input in case the user does not follow the program rule(only 10 bytes in one row). And then request the user for another input, take the first 10 bytes and ignore the rest as well.
    How to handle this?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    fgets(buf,11,stdin);
    if( strchr(buf,'\n') == NULL)   {  // no newline in buf
        int c;
        while( (c=getchar()) != EOF && c != '\n')
             ;
    //    error("input longer than 10 chars");
      
    }
    Last edited by Bayint Naung; 10-29-2010 at 03:07 AM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    Thanks for your codes, but I don't want to validate the input and throw out an error message to terminate my program, I just want to ignore the useless user input(those characters beyond the first 10 bytes).
    I know one solution could be that I make another fgets function call if I detect that user input is longer than what I need and simply ignore it. However, this still needs to validate the input and it also has some overhead to make a fgets function call that simply eat up those rubbish data, this way as what I said in the previous post, it is ugly.
    I just wonder if there is any other ways.
    Last edited by icoigo; 10-29-2010 at 03:52 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the way I'd do it:

    Code:
    #include <stdio.h>
    
    int main() {
      int i,j; 
      
      char str[3][10+1];
      char line[100];
    
      printf("\n\n\n");
      for(i=0;i<3;i++) {
         printf("\nEnter a line of text: ");
         fgets(line, sizeof(line), stdin);
         line[10]='\0';
         strcpy(str[i], line);
         line[0]='\0';  //purely defensive
      }
      for(i=0;i<3;i++) 
        printf("\nstr[%d]: %s", i+1, str[i]);
    
      printf("\n\n\t\t\t     press enter when ready");
    
      (void) getchar(); 
      return 0;
    }
    If the user is going to mess with you, beyond a 100 char's per line, let me know - we'll go "industrial" strength on 'em!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > The real problem is, I want to store user input into a char[], so I can allocate memory as char[10],
    The real problem is you're assuming that the user enters correct data.

    The actual steps you should be performing are:
    - read the user input into a temporary buffer, taking care of I/O errors.
    - validate the input (length, content, whatever)
    - store the valid input where you want to keep it.

    This is normally achieved by declaring the temporary buffer to be BUFSIZ in length.

    If all you want is the first 10 chars, then that's an easy strncpy().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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