|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
I have the following code written to search a document for occurances of a string ( eventually will be searching a folder of documents, but thats another problem)
This code works perfect when compiled in Visual C++ but when I try to convert to cgi, the first strstr line lets me down......absolutely lost on this one...... Cheers............................PJG #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXLEN 80 #define EXTRA 5 /* 4 for field name "data", 1 for "=" */ #define MAXINPUT MAXLEN+EXTRA+2 /* 1 for added line break, 1 for trailing NUL */ void unencode(char *src, char *last, char *dest) { for(; src != last; src++, dest++) if(*src == '+') *dest = ' '; else if(*src == '%') { int code; if(sscanf(src+1, "%2x", &code) != 1) code = '?'; *dest = code; src +=2; } else *dest = *src; *dest = '\n'; *++dest = '\0'; } int main(void) { /* begin program */ char line[250]; const char *delimiters = {" \t \n "}; char *token=NULL; char* pStr = NULL; int count = 0, line_count=0; char *test = ""; FILE *essay; char *lenstr; char input[MAXINPUT], data[MAXINPUT]; long len; printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10); printf("<TITLE>Response</TITLE>\n"); lenstr = getenv("CONTENT_LENGTH"); /* Getting string from HTML page */ if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN) /*verifying that string is valid*/ printf("<P>Error in invocation - wrong FORM probably."); else { /* begin ELSE */ fgets(input, len+1, stdin); unencode(input+EXTRA, input+len, data); if ((essay = fopen("C:/Inetpub/wwwroot/cgi-bin/stroke.txt", "r")) == NULL ) { printf("<h2>File could not be opened - ask yourself WHYYYYY?"); } else while (!feof(essay)) { if( fgets(line, 250, essay) != NULL ) { pStr = line; line_count++; if (( test = strtok(pStr, delimiters )) != NULL ) { if(strstr(test,data) !=NULL) { count++; printf("<P>Entry %d on line %d: %s<br>", count, line_count, data); } while (( test = strtok((char *)NULL, delimiters )) != NULL) { if ( strstr(test,data) != NULL) { count++; printf("<P> Entry %d on line %d: %s<br>", count, line_count, data); } } } } } printf("<H3>Files were searched successfully.... how about that!"); printf("<P>The string <H4>%s</H4> occurs %d times in %s <BR>", data, count, essay); fclose(essay); count =0; } return 0; } /* End Program */ |
|
#2
|
|||
|
|||
|
Does it work if you do not call unencode() ?
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > help with CGI script in C |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|