akohan
asked on
how to use strstr?
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
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));
Is your heystack null terminated?
ASKER
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.
ASKER
heystack is a char*
>> 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 ?
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 ?
btw, is this the assignment you intended :
htmlcontent = buf;
?? Or was it supposed to be the other way around ?
htmlcontent = buf;
?? Or was it supposed to be the other way around ?
>> However, <title> is in the buf.
Oh, and notice that you're looking for "<TITLE>", not "<title>" - note the difference in case :)
Oh, and notice that you're looking for "<TITLE>", not "<title>" - note the difference in case :)
ASKER
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);
}
>> as I dump it I can see the <title> tag.
"<title>" or "<TITLE>" ? They're not the same.
"<title>" or "<TITLE>" ? They're not the same.
ASKER
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
ASKER
Best C programming adviser ever!
Your code
can crash if the html content has any "%" characters in it. This works instead:
if(htmlstart){
fprintf(stdout, htmlcontent);
}
if(htmlstart){
fprintf(stdout, "%s", htmlcontent);
}
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)