Thread: fgets

  1. #1
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    fgets

    'lo everybody...

    When using fgets() is there a way to stop it adding the newline character to the end of my input?

    Erm... that's all... any help is appreciated...

    Thanks
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It only takes one line to get rid of the newline

    Code:
    fgets( buff, BUFSIZ, stdin );
    
    // find the newline, and if its there, overwrite it with \0
    char *p = strchr( buff, '\n' );  if ( p != NULL ) *p = '\0';
    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.

  3. #3
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Makes sense, thanks again Salem....
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An alternative to Salem's code that does the same thing:
    Code:
    fgets(buf, BUFSIZ, fp);
    buf[strcspn(buf,"\n")] = '\0';
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    strcspn?

    That's a nice one Hammer. Thanks for showing the alternative.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Hammer, could you please stop advising people to use strcspn() instead of strchr()?
    You didn't read my post properly then? I wasn't advising them to use it, only showing another way to do the same thing (just like I said).
    And I'll post what I like, thanks.

    >>There are a thousand ways of doing this and the 10 bytes (or so) of program text it saves aren't so significant that you have to convince everybody of using it
    Again, I'm not trying to convince anyone. Knowing more than one day to do something is an advantage in itself.

    >>(in fact, the strchr() version is faster ;P).
    Really, now that I didn't know. Can you back that one up with some evidence (yes, I'm serious, this is not a sarcastic comment, I am interested to know which would be the better way).

    >>That's a nice one Hammer. Thanks for showing the alternative.
    No problem, at least someone appreciates it!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I didn't mean to make a discussion of this
    Why not, that's what these boards are for! I don't take your comments personally, and I promote open conversation

    >...but strcspn() just can't be done as fast.
    Thinking about it, the answer is obvious. With the second parameter of strcspn() being a list of chars, it's clear that the function will need to loop through them, whereas strchr() has to work with only one char.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    I get 771, 1942 for VC++ 6.0, but this is without optimization. But seriously, if you have to run it 10000000 each to get these tests does it really matter which one is faster? Sometimes yea, but most of the time it's pointless, write a program that's clear and easy to read from the start and if there're speed issues then deal with them after the program is working. I've read all over the place that optimizing little things for almost no speed gain is stupid unless you need to. I know of four ways to do this now. Naturally I like the one that's the easiest to read and figure out without looking over more code than I have to.
    Code:
    char *p = strchr( buff, '\n' );  if( p != NULL ) *p = '\0';
    Code:
    buff[strcspn( buff, "\n" )] = '\0';
    Code:
    if( buff[strlen( buff ) - 1] == '\n' ) buff[strlen( buff ) - 1] = '\0';
    Code:
    char *p = strpbrk( buff, "\n\0" ); *p = 0;
    /* Edited */
    Last edited by Dr. Bebop; 09-11-2002 at 10:04 AM.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  9. #9
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    I think this is a good discussion, not pointless. I'm learning a lot by watching you guys explain your take on things.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    He Dr. Bebop, you forgot one:
    Code:
    char *p = strpbrk(buf, "\n\0"); *p = 0;

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I think this is a good discussion, not pointless.
    Maybe the subject does seem trivial, but it does promote a good convo! Which is more than I can say for a lot of threads (like "where can I find this...", and "how do I do that...")

    >>Just a matter of personal choice
    yes, exactly, which is how we started, by giving someone a choice.

    >>I'm learning a lot by watching you guys explain your take on things.
    Good
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    If there is no newline in the array will strcspn put a null terminator at the end of the array?

  13. #13
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    My manual says no. The two parameters passed in should already be null-terminated.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Troll_King
    If there is no newline in the array will strcspn put a null terminator at the end of the array?
    No, strcspn simply returns the number of characters in the first arg that are not in the second arg (starting from the beginning).

    So, if you do strcspn("hello", "o") the return will be 4. If no match is found at all, then the number returned is the number of chars in the array, not including the null terminator. Eg:
    strcspn("hello", "x") will return 5.

    This makes it an ideal function to put into the array [ ] to get at a specific index, as per the example discussed here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    So that's why you can do the one liner? If there's a newline then it's relplaced by null and if there isn't then strcspn returns the index of the trailing null, so the string doesn't change? Cool
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

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