?
Solved

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

Posted on 2003-03-12
5
Medium Priority
?
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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