?
Solved

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

Posted on 2003-03-12
5
Medium Priority
?
394 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:PeteG
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8121777
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);
0
 

Author Comment

by:PeteG
ID: 8121968
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

0
 
LVL 6

Accepted Solution

by:
gj62 earned 80 total points
ID: 8122064
Sorry, malloc() returns void *, so you have to cast it to a char *.  Change the malloc() statement to:

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

Author Comment

by:PeteG
ID: 8122132
Thanks a mil, sorted, sorry again about the low points.

0
 
LVL 6

Expert Comment

by:gj62
ID: 8122153
Thank you - points were fair for the question - no worries.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question