Thread: strlen adds +1 to length when using fgets

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    strlen adds +1 to length when using fgets

    If I type "abc" using
    Code:
    scanf("%s" , s1);
        printf("%d\n" , strlen(s1));
    I get "3" which is correct. But when using
    Code:
    fgets(s1,10,stdin)
    I always get the number of characters I type in +1. Why?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    646
    You do type 'a', 'b', 'c' and ENTER, didn't ya?
    Well... fgets() don't strip this final '\n'... Your string is "abc\n\0".

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Quote Originally Posted by flp1969 View Post
    You do type 'a', 'b', 'c' and ENTER, didn't ya?
    Well... fgets() don't strip this final '\n'... Your string is "abc\n\0".
    Yeah I do, I get it, should i write
    Code:
    printf("%d\n", strlen(s1)-1);
    gets(); gives a warning and scanf wont register spaces.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    646
    Quote Originally Posted by mangekyou View Post
    Yeah I do, I get it, should i write
    Code:
    printf("%d\n", strlen(s1)-1);
    gets(); gives a warning and scanf wont register spaces.
    A simple solution is, after your fgets() call do:
    Code:
    {
      char *p;
      if ( p = strchr( s1, '\n' ) ) *p = '\0';
    }
    This will strip the '\n' from the string.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    A more elegant solution I like to use to remove the trailing newline character is:
    Code:
    s1[strcspn(s1, "\n")] = '\0';

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    646
    Quote Originally Posted by christop View Post
    A more elegant solution I like to use to remove the trailing newline character is:
    Code:
    s1[strcspn(s1, "\n")] = '\0';
    I agree, but notice if there is no '\n' in the string, the final '\0' will be overwriten with '\0' again (this is unecessary)... I know, it works...
    The strchr() approach changes '\n' only if it is found. (it is a little bit faster!).

    Of couse it is more code in C, but if you want to write less code, one can write a simple macro or inline function:
    Code:
    #define STRIPNL(s) { char *p; if ( p = strchr( (s), '\n' ) ) *p = '\0'; }
    And use it:
    Code:
    STRIPNL(s1);
    Last edited by flp1969; 04-26-2019 at 06:52 PM.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Quote Originally Posted by christop View Post
    A more elegant solution I like to use to remove the trailing newline character is:
    Code:
    s1[strcspn(s1, "\n")] = '\0';
    This is my favourite when it comes to fgets - Think about it this way: when will fgets not have a new line character? When it returns NULL - in that case you won't be doing the operation.

    You can also add "\n\r" if the stream is binary.


    Just a style thing here - I'd make stripnl a function, because macros are a pain in the ... when trying to debug - Especially when the code is large and written by someone else that has left the company...

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Thanks! There is still another problem with using fgets. If I want to enter and store a password using fgets, it will add a 0 at the end of my password? How do I make it not count the 0 at the end?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,549
    The 0 is the null character used to terminate the string. It already isn't counted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-15-2015, 06:18 PM
  2. Array length error using strlen()
    By wirefree101 in forum C Programming
    Replies: 3
    Last Post: 11-11-2009, 10:11 PM
  3. strlen does not equal string length
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 02:59 PM
  4. getting string length without using strlen
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2001, 11:11 PM
  5. Count the length of strintg without using strlen?
    By yukon in forum C Programming
    Replies: 9
    Last Post: 10-01-2001, 08:32 AM

Tags for this Thread