Thread: Filtering out comments.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Filtering out comments.

    I have a block of code that reads a file line by line looking for a specific character combination contained within the line. It works fine.

    Code:
    while (fgets(line, sizeof(line), file ) != NULL) {
        match = strstr(line, CHECKSTR);
        if (match == NULL) {
            // Line does not contain substring 
        }
        else {
            // Line contains the substring
        }
    }
    What I want to do though is skip over any line that is a comment, since some lines that are commented out, may contain the substring I'm trying to match. The comment character is the standard # character, but I have to take into account that it may not be the first character on the line. The line could, for example be indented with spaces or tabs before the comment character occurs.

    So I'm now thinking about the best way to go about this. I could either loop through a series of strcmp to determine whether or not I hit any other character besides a whitespace character or the # character, or I could try to use strok() to accomplish this. I need to read up on that function more. I'm still a noob.

    So my question is what's the best way to go about this? Best being most efficient. I'm not asking for code, just how would you go about that if you had to do this?

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok so the comment character (an octothorpe) has to be the first non-whitespace character on a line...

    Code:
    int IsComment(char *str)
      { char *ptr = str;
         // advance to first non-whitespace character
         while(isspace(*ptr++));
         // is this a comment?
         if (*ptr) == '#'
           return 1;  // true
        return 0; }  // false
    
    
    // read in a line from the file
    if (! IsComment(Line))
      { //do what ya gotta do 
    }
    When you think in small blobs things get alot easier.
    Last edited by CommonTater; 04-28-2011 at 08:24 PM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could just ignore the line by doing nothing once you have identified it with something like this.

    Code:
    if( strchr(line, '#') )
        ;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Subsonics
    You could just ignore the line by doing nothing once you have identified it with something like this.
    That won't actually work though, since it will ignore all lines with a comment, even those that contain uncommented out code. Furthermore, if '#' can occur in a context other than a comment, such a line will also be ignored.
    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

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, that is the most simple example, it wasn't clear from the post what was the requirement. You could follow it up by a stpbrk() call if there are required characters such as ; in C for a valid line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. packet filtering and/or firewalls
    By obsidian in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-09-2003, 03:16 AM
  2. comments
    By nightingale in forum C Programming
    Replies: 32
    Last Post: 07-18-2003, 03:49 AM
  3. Moving/Filtering JPEG files?
    By Aaron Buckley in forum Windows Programming
    Replies: 6
    Last Post: 01-24-2003, 10:42 PM
  4. help needed - filtering a data file (with strings?)
    By Galligan in forum C Programming
    Replies: 1
    Last Post: 11-18-2002, 10:20 AM