Thread: problem with fgets

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    20

    problem with fgets

    Hi all, can anybody help me solve the problem below? If I enter a string with a string length of more than 10 for inputString, the next fgets will just take what is stored in the buffer. I searched the forum for problems with fgets, but only found out that if i use fgets, and if it exceeds the buff size, the next fgets will pick up what is left from the first fgets. How can I solve this? Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 10
    
    int main(void)
    {
       char *inputString, *inputValue;
       
       inputString = malloc(MAXSIZE);
       inputValue = malloc(MAXSIZE);
       
       if(inputString==NULL)
       {
          fprintf(stderr, "Unable to allocate memory");
          exit(1);
       }
       
       if(inputValue==NULL)
       {
          fprintf(stderr, "Unable to allocate memory");
          exit(1);
       }
    
       printf("Please enter a string: ");
       fgets(inputString, MAXSIZE, stdin);
       
       printf("Input a value: ");
       fgets(inputValue, MAXSIZE, stdin);
       
       free(inputString);
       free(inputValue);
       
       return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Easiest solution... you probably should increase MAXSIZE to a level you feel will help you accommodate the largest possible string that would ever be entered by the user. If you anticipate the user may sometimes enter up to 30 characters, then you should make MAXSIZE at least 31 (+1 for the NULL) for example.

    Alternately, switch to using a C++ string container. They would allow strings of any arbitrary length to be entered.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( buf, BUFSIZ, fp ) )
    {
        if( strchr( buf, '\n' ) )
            break;
    
        ...otherwise, realloc some space, strcat this buffer onto it...
    }
    Something like that should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Ok, I got it, thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 10:13 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. fgets problem
    By gambitmj in forum C Programming
    Replies: 5
    Last Post: 02-26-2002, 08:55 AM