int main(void) { int expecting_lbrace = 0, expecting_rbrace = 0; int expecting_lparen = 0, expecting_rparen = 0; int c, expecting_scolon = 0; unsigned int ii; char string[64]; FILE *conf_file; if (!(conf_file = fopen("test.conf", "r"))) { printf("Error opening test.conf: %d (%s).\n", errno, strerror(errno)); return 0; } while ((c = fgetc(conf_file)) != EOF) { switch (c) { case '{': if (!expecting_lbrace) { printf("Unexpected ' { ' reached\n"); getchar(); exit(0); } expecting_lbrace--; expecting_rbrace++; break; case '}': if (!expecting_rbrace) { printf("Unexpected ' } ' reached\n"); getchar(); exit(0); } expecting_rbrace--; break; case '(': if (!expecting_lparen) { printf("Unexpected ' ( ' reached\n"); getchar(); exit(0); } expecting_lparen--; expecting_rparen++; break; case ')': if (!expecting_rparen) { printf("Unexpected ' ) ' reached\n"); getchar(); exit(0); } expecting_rparen--; break; case ';': if (!expecting_scolon) { printf("Unexpected ' ; ' reached\n"); getchar(); exit(0); } break; case '\"': ii = 0; while ((c = fgetc(conf_file)) != '\"') string[ii++] = c; string[ii] = 0; printf("string: %s\n", string); expecting_lbrace = expecting_lparen = 1; break; default: /* Assume it's a whitespace character */ break; } } return getchar(); }