excluding characters

This is a discussion on excluding characters within the C Programming forums, part of the General Programming Boards category; fgets stops when it sees newline and stores it, so I think you need simply something like this: Code: size_t ...

  1. #16
    mzn
    mzn is offline
    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
    21,326
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    mzn
    mzn is offline
    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
    21,326
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21