Solved

how to use strstr?

Posted on 2008-10-09
14
1,295 Views
Last Modified: 2012-05-05
Hello group,


I'm trying to parse an html tag in an array assume called "buf". buf has the contents of an html file I have read from a server. Now, when I use strstr() to find a string or tag in there I get segmentation as well. I thought this example will be fine but it is not.

Can you please tell me what is the best approach here?


Any help is appreciated.

thanks,
ak



htmlcontent = buf;
 
char* needle = "<TITLE>";
char* heystack = buf;
 
printf(">>>The first character found: %c", strstr(heystack, needle));

Open in new window

0
Comment
Question by:akohan
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22682322
strstr returns a char*, which is either NULL (in case the string is not found) or points to the firs tlocation where the string is found.

So there are two problems with that code :

1) You have to check the return value of strstr for NULL, and NOT print it if it's NULL

2) you have to use the proper %s format (%c is for a single character)
0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22682324
Is your heystack null terminated?
0
 

Author Comment

by:akohan
ID: 22682357

sorry I forgot to say that I solved that issue my recent issue is that I am not getting anything. However, <title> is in the buf.

Any comments?

Regards.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:akohan
ID: 22682360

heystack is a char*

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22682368
>> Any comments?

The comments I already made ;)


Can you show the exact code you use, as well as how you verified that "<title>" was really in buf ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22682384
btw, is this the assignment you intended :

        htmlcontent = buf;

?? Or was it supposed to be the other way around ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22682407
>> However, <title> is in the buf.

Oh, and notice that you're looking for "<TITLE>", not "<title>" - note the difference in case :)
0
 

Author Comment

by:akohan
ID: 22682449

I verify it by looking at htmcontent's contents, as I dump it I can see the <title> tag.
also, when I use %s I get NULL.

Thanks!



while((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0){
          if(htmlstart == 0)
          {
            htmlcontent = strstr(buf, "\r\n\r\n");
            if(htmlcontent != NULL){
              htmlstart = 1;
              htmlcontent += 4;
            }
 
          }else{
 
            htmlcontent = buf;
            printf("\n\n\n\n***********************\n");
            char* needle = "<TITLE>";
            char* heystack = buf;
 
            printf(">>>The first character found: %s", strstr(heystack, needle));
            printf("\n***********************\n");
          }
 
          if(htmlstart){
            fprintf(stdout, htmlcontent);
          }

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22682474
>> as I dump it I can see the <title> tag.

"<title>" or "<TITLE>" ? They're not the same.
0
 

Author Comment

by:akohan
ID: 22682481

I tried both TITLE and title but got the same result. however, that's my other question should I use isupper()? but as far as I remember that is for character not string!
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 22682502
Can you add this line just before the line where you call strstr :

        printf("\nHTML :\n%s\n\n", heystack);

and post the output here ?
0
 

Author Comment

by:akohan
ID: 22683195

Hi Infinity08,

I found I had put the code in a wrong block (since I'm using a loop) so I was missing the first segment of the retrieved html body finally got it by moving it to the right section of the code.

Thanks!
ak
0
 

Author Closing Comment

by:akohan
ID: 31504829

Best C programming adviser ever!
0
 

Expert Comment

by:geoffff
ID: 24446678
Your code

Open in new window

    if(htmlstart){

Open in new window

      fprintf(stdout, htmlcontent);

Open in new window

    }

Open in new window


Open in new window

can crash if the html content has any "%" characters in it.  This works instead:

Open in new window

    if(htmlstart){

Open in new window

      fprintf(stdout, "%s", htmlcontent);

Open in new window

    }

Open in new window


Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why adding test code changes the build ? 11 132
chcp 65001 File encoding 66 267
Please explain C language error codes ? 5 26
C qsort compare function issue 6 14
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…
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 structures 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.

809 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