Link to home
Start Free TrialLog in
Avatar of PeteG
PeteG

asked on

strtok -- how to revert to original string after processing????(Sorry points are low!!)

I have a code that takes a two word string and searches through documents for occurences of each string and returns how many times each occured.... problem is when I enter the second txt file, the string has been tokenised, i.e. only the first string is passed through the other docs, how do I revert to the original string for processing of the other files??

Here is the code....(its a CGI/C script)
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.</P>");


     else {   /* begin ELSE */

     fgets(input, len+1, stdin);
    unencode(input+EXTRA, input+len, data );  /* use function unencode to get actual string */
       
           
         
         
               

         
         for ( int i = 0; i < 3; i++)

          {   // begin of opening each file
       
          if((essay = fopen( names[i] , "r+")) == NULL )
          {
               fprintf(stderr, "Error opening files\n");
               return 0;
          }



       
                 
                     
                 entry = strtok ( data, " ");
                 //entry2 = strtok ( data, " ");    

                 while (( entry != NULL) && ( strlen(entry) > 2))
                 
                 {
                           
                         

                              while (((fscanf(essay, "%s", word) == 1)) )
                            // begin WHILE at fscanf
               
                           
                               if (strcmp(entry, word) == 0)
                     
                               
                                count++;
                     
                   
                     
                                printf("%s occurred %d times in %s<br>", entry, count, names[i]);
                               
                                count = 0;
                     
       
                                // fputs(entry, results);
                   
                                entry = strtok(NULL, " ");
                   
                               fseek(essay, 0, 0);
                   
                   
                 }
                         


       

          fclose(essay);  /* closing text file */
           
         
          } // on to next file
     ........ etc etc etc end of program

Rgds PeteG
Avatar of gj62
gj62

You have to make a copy...

char *copyData;

copyData = malloc(strlen(data)*sizeof(char)+1);
strcpy(copyData,data);

....
Do whatever you need to, then "restore" data...
....

strcpy(data, copyData);
Avatar of PeteG

ASKER

gi62,

Thats just what I want to do, but I'm still getting an error -- -- I think the problem might be at the declaration of 'data'
this is the error message
C:\Inetpub\wwwroot\cgi-bin\finalCGIsearch\cgi2Search.cpp(79) : error C2440: '=' : cannot convert from 'void *' to 'char *'

Here is the declaration section....

#include <stdio.h>
#include <string.h>
#include <ctype.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) /* code for converting the string inputted back to its regular form */
{
 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 */




int count = 0;   /* counters */
FILE *essay;
char *names[] = {"C:/Inetpub/wwwroot/cgi-bin/diabetes.txt", "C:/Inetpub/wwwroot/cgi-bin/stroke.txt", "C:/Inetpub/wwwroot/cgi-bin/stemcells.txt" };
char *lenstr;          /* pointer to the string received from the HTML page */
char input[MAXINPUT];  /* input received from the HTML page */
long len;
char data[MAXINPUT];  /* the unencoded input from the HTML page */
char *copyData;
char word[40];


char *entry;

printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);

... regards

PeteG

ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeteG

ASKER

Thanks a mil, sorted, sorry again about the low points.

Thank you - points were fair for the question - no worries.