Thread: How to preserve the benefit of fgets?

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    How to preserve the benefit of fgets?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char name[3], *line;
    
        printf("Name: ");
        fgets(name, sizeof(name), stdin);
        // Remove newline
        line = strchr(name,'\n');
        *line = '\0';
        printf("%s\n", name);
        printf("sssssssssssssss\n");
        system("pause");
    }
    As I add the code of remove newline, after input very long name it will show unhandled exception. How can I remove the newline and still get the benefit of fgets at the same time?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Like this:
    Code:
    if (fgets(name, sizeof(name), stdin))
    {
        char *line = strchr(name, '\n');
        if (line)
        {
            *line = '\0';
        }
    }
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps by writing a function with the same interface?
    Code:
    char *my_fgets( char *buffer, size_t size, FILE *fp ) {
      char *result = fgets( buffer, size, fp );
      if ( result ) {
        // remove \n
      }
      return result;
    }
    > As I add the code of remove newline, after input very long name it will show unhandled exception. How can I remove the newline and still get the benefit of fgets at the same time?
    If the line is very long, then it will NOT have a \n.
    Without a \n, your strchr() returns NULL - and you can guess what happens when you then try *line = '\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.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Thank you very much.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char name[3], line;
    
        printf("Name: ");
        fgets(name, sizeof(name), stdin);
    
        line = strchr(name,'\n');
            
    
        printf("%sAAAAAAAAAAA\n", name);
        printf("sssssssssssssss\n");
        system("pause");
    }
    It still works fine after I remove *line = '\0';. I think *line = '\0' is redundant.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try typing in only one character, then press enter.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Why become like that? @@

    How to preserve the benefit of fgets?-2-jpg

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because now that you are not setting the newline character to '\0', it is displayed when printed, and the way to display a newline is to... print a newline.
    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

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    I wonder when face with character need to use \0 but doesn't need \0 when it is string.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ulti-killer
    I wonder when face with character need to use \0 but doesn't need \0 when it is string.
    What do you mean?
    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

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Quote Originally Posted by laserlight View Post
    What do you mean?
    How does *line = '\0' works when input 1 character such as a.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ulti-killer
    How does *line = '\0' works when input 1 character such as a.
    Basically, your string looks like this:
    Code:
    {'a', '\n', '\0'}
    Then you replace the '\n' with '\0' to get:
    Code:
    {'a', '\0', '\0'}
    The reason why Salem asked you to enter one character is that you declared name to be an array of 3 characters, so you can only enter 1 letter for the name if you want to see the effect of the replacement. Otherwise, you may end up with:
    Code:
    {'a', 'b', '\0'}
    upon which no replacement happens because the '\n' is not found.
    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

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Quote Originally Posted by laserlight View Post
    Basically, your string looks like this:
    Code:
    {'a', '\n', '\0'}
    Then you replace the '\n' with '\0' to get:
    Code:
    {'a', '\0', '\0'}
    The reason why Salem asked you to enter one character is that you declared name to be an array of http://cboard.cprogramming.com/clear.gif Reply With Quote3 characters, so you can only enter 1 letter for the name if you want to see the effect of the replacement. Otherwise, you may end up with:
    Code:
    {'a', 'b', '\0'}
    upon which no replacement happens because the '\n' is not found.
    Code:
    {'a', '\n', '\0'}
    Why only one character generates '\n'?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ulti-killer
    Why only one character generates '\n'?
    Because fgets stores the '\n' when there is space.

    Okay, let's move away from this "one character" fixed notion. Compile and run this program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
        char name[13], *line;
     
        printf("Name: ");
        fgets(name, sizeof(name), stdin);
     
        line = strchr(name,'\n');
             
     
        printf("%sAAAAAAAAAAA\n", name);
        printf("sssssssssssssss\n");
    }
    Notice that this is the same program as you posted in post #5, except that I changed the 3 to 13, and fixed line to be a pointer instead of a char. Enter: "hello world" and observe the output. Then run the program again, entering "hello world!" and observe the output.
    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

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Charity Race to Benefit Medical Research May 15 - 25th
    By Adak in forum General Discussions
    Replies: 1
    Last Post: 05-27-2012, 04:42 AM
  2. Concerning gets() and fgets()
    By ozumsafa in forum C Programming
    Replies: 20
    Last Post: 07-19-2007, 05:32 AM
  3. How to preserve background after TextOut?
    By Sebastiani in forum Windows Programming
    Replies: 9
    Last Post: 03-29-2002, 01:36 PM
  4. what is benefit of using window NT?
    By Mane in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-29-2002, 08:13 AM
  5. For the Benefit of Mr. Kite
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-25-2001, 06:24 AM