Thread: fgets problem

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    fgets problem

    hi,

    i using fgets() to get input from a user...

    printf("Address [street no and name]: ");
    fgets(cr->address_street, 40, stdin );

    it works great - however, when it stores a string into my variable (cr->address_street) and also when i want to print the contents of the variable onto the screen - i find that my variable contains exactly 40 characters including a '\n' at the end, regardless of whether the user input string was 20 or 30 or x characters.

    is there an easy way to get rid of the whitespace and newline after the data, which is automatically stored by fgets since i specified the length of string to read? please let me know if this is ambiguous...

    thanks!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    fgets() shouldn't leave no whitespace after '\n'... sure your code is alright? Anyway, to get rid off that newline you must have a code by yourself. ...But 'cause I'm such a nice guy and all that...:

    Code:
    inline void remove_nl(char *s)
    {
    	if(s[strlen(s)-1] == '\n')
    		s[strlen(s)-1] = '\0';
    }
    (Remember that instead of garbage after '\0' you will now have another '\0' followed with garbage).

    ** This is my 100th post :-) **

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    You could use gets(); instead remember fflush(stdin) to flush the buffer...or use getch(); in <conio.h> to get string character at a time.

    Code:
    char string[40];
    int i;
    
    printf("Enter a string :");
    i = 0;
    do
    {
       string[i]=getch();
       if(string[i] =='\r') break;
       i++;
    } while(i < 39);
    string[i]='\0';  /*add null*/
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!
    Note that getch() is not ANSII conform!

    klausi
    When I close my eyes nobody can see me...

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Question redefinition of my fgets problem

    hi,

    thanks for everyone who is trying to help. i probably didn't explain my problem clearly enough - let me try again...

    i'm using fgets() to get input from a user using the following syntax:

    printf("Address [street no and name]: ");
    fgets(cr->address_street, 40, stdin );

    my problem isn't with the '\n' at the END of the string... my problem is with all the '\0' or whitespace in front of the '\n'. because i am telling fgets to read in a string of no longer than 40 characters, it automatically reads in 40 characters even when the user input is only 20 characters. here's an example:

    if the user input is 123 Main Street, my variable... contains '123\0Main\0Street\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\n'.

    is there an easy way to chop off the '\0's that fgets automatically includes when the user input is less than the specified length? admittedly, this is merely a formatting problem - should i even care about the whitespace and just get rid of the newline and be happy?

    thanks...

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Okay,
    once the user has entered whatever he likes, it doesnot really matter what comes after the final '\0', as this is just whatever was in the buffer prior to the user entry.

    if the user entered '123 Main Street' then the actual memory allocated for the variable will contain '123space character ' 'ascii32Main space character ' 'ascii32Street\0plus what is in the buffer'
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

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. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  4. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM