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