// Ivan Novick // Feb-28, 2011 // demonstrate typical config file parsing // where you remove # comments and // trim leading and trailing white space #include #include char* normalize(char* line); int main(int argc, char** argv) { char buffer[1024] = { 0 }; char* comment = NULL; FILE* fp = stdin; while (NULL != fgets(buffer, 1024, fp)) { buffer[strlen(buffer)-1] = 0; // print the line after removing comments and // trimming white space printf("%s.\n", normalize(buffer)); } fclose(fp); return 0; } // remove comments with # // remove trailing and leading spaces char* normalize(char* line) { int i; int len = 0; char* comment; // remove comments with # comment = strstr(line, "#"); if (comment) { // everything in the string from comment char unward is ignored *comment = 0; } // remove trailing spaces len = strlen(line); for (i = len-1; i >= 0; --i) { if (isspace(line[i])) line[i] = 0; else break; } while (*line) { if (isspace(*line)) line++; else break; } return line; }