Hi all.
I'm having some trouble with some simple C code. It looks absolutely fine to me and a couple of other people I've asked, but the compiler is insisting there's something wrong with it.
Here's the code:
I get this error:Code:/* highlighter.c */ #include <stdlib.h> #include <stdio.h> #include <string.h> void printtofile(char data[128]) { FILE *f = fopen("high_log.log", "a"); fprintf(f, data); fprintf(f, "\n"); fclose(f); } /* function to highlight certain words in an HTML string */ char *highlight(char *htmlstring, char *word, char *highlightedstring) { char datastring[128]; sprintf(datastring, "string to highlight is '%s', word is '%s'", htmlstring, word); printtofile(datastring); char *currentchar = htmlstring; //error is on this line int wordlen = strlen(word); char *htmlstringend = htmlstring + strlen(htmlstring); char *openbracket = strchr(currentchar, (int)'<'); char *closebracket = strchr(openbracket, (int)'>'); char *matchedword = strstr(currentchar, word); const char *highlighttagstart = "<font color=\"#FF0000\">"; const char *highlighttagend = "</font>"; const int highlighttagslength = strlen(highlighttagstart) + strlen(highlighttagend); highlightedstring[0] = '\0'; while( *currentchar && (currentchar < htmlstringend) ) { if ( openbracket < matchedword ) { currentchar = closebracket + 1; openbracket = strchr(currentchar, (int)'<'); closebracket = strchr(openbracket, (int)'>'); if ( closebracket > matchedword ) { matchedword = strstr(currentchar, word); } continue; } /* at this point, we should have a match */ strcat(highlightedstring, highlighttagstart); strncat(highlightedstring, currentchar, wordlen); strcat(highlightedstring, highlighttagend); currentchar+=4; }; return( highlightedstring ); }
highlighter.c:24: parse error before `char'
I'm compiling this on Linux using gcc. I don't think that should be any problem.
Anybody see any syntax errors? The syntax errors go away if I remove the first 3 lines of the highlight() function (they are just for logging) but the reason I'm trying to use a log is because I'm getting run-time errors.




Reply With Quote