Thread: excluding characters

  1. #16
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    fgets stops when it sees newline and stores it, so I think you need simply something like this:
    Code:
    size_t len;
    while(fgets(buffer, sizeof buffer, f)){
        len = strlen(buffer);
        if(buffer[len-1] == '\n')
            bufferl[--len] = '\0';
        ...
    }
    Last edited by mzn; 07-08-2010 at 11:24 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mzn
    fgets stops when it sees newline and stores it, so I think you need simply something like this:
    Yes, that can work, but it is simpler to use strchr instead of strlen.
    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. #18
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Yes, that can work, but it is simpler to use strchr instead of strlen.
    I don't think he gets away any easier using strchr. But what I do think is that strlen return value just might be more of use later in his code, thats only my guess though

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mzn
    I don't think he gets away any easier using strchr.
    It is less work, both in the code and in what you have to consider, i.e., whether array out of bounds access is possible. For example:
    Code:
    while (fgets(buffer, sizeof buffer, f)) {
        char *p = strchr(buffer, '\n');
        if (p)
            *p = '\0';
        /* ... */
    }
    Quote Originally Posted by mzn
    But what I do think is that strlen return value just might be more of use later in his code, thats only my guess though
    Both are true: the length of the string may be useful, but it may also be unnecessary.
    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. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM