|
-
July 31st, 2004, 07:26 PM
#1
Parsing
Code:
CString ID_Parser(CString StrText)
{
int Blah1 = strstr(StrText, "<td><font face=arial size=\"-1\" color=\"#660000\"><b>");
int Blah2 = strstr(StrText, "</b></font>");
StrText = StrText.Mid(0, Blah1 + 50);
StrText = StrText.Left(Blah2 - 1);
return StrText;
}
This obviously doesnt work... Im trying to write a function to parse the ID from a yahoo profile, this is what I have so far but isnt working because of strstr(), Im wondering if there is an alternative function (maybe in the CString class) that will return the postition of a certain string, like vb's InStr ? or if you can show me another method it would be much appreciated!
Thanks
-
July 31st, 2004, 09:59 PM
#2
Hi, leet3r,
What do you mean by "it doesn't work because of strstr()"?
It is an MS extension, but to say, that it doesn't work...
Thank you.
Last edited by OneEyeMan; July 31st, 2004 at 10:01 PM.
-
July 31st, 2004, 11:07 PM
#3
Im wondering if there is an alternative function (maybe in the CString class) that will return the postition of a certain string...
int CString::Find( LPCTSTR pstr, int nStart ) will do that.
Return Value:
The zero-based index of the first character in this CString object that matches the requested substring or characters; -1 if the substring or character is not found.
-
August 1st, 2004, 05:28 AM
#4
Hi,
OneEyedMan: I meant that the problem with my function is the use of strstr() if that makes any sense :P
Im going to see what I can do with CString::Find now, thanks! much appreciated.
-
August 1st, 2004, 11:03 AM
#5
There is no problem with strstr.
The trick is to actually get the character position from return
here from msdn:
Code:
printf( "String to be searched:\n %s\n", string );
printf( " %s\n %s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = (int)(pdest - string + 1);
if ( pdest != NULL )
printf( "%s found at position %d\n", str, result );
else
printf( "%s not found\n", str );
cheers
The most knowledgeable people are those who know that they know nothing.
-
August 1st, 2004, 12:41 PM
#6
Here is what I have so far..
Code:
CString ID_Parser(CString StrText)
{
if(strstr(StrText, "Yahoo! ID:"))
{
char *Blah1 = strstr(StrText, "<td><font face=arial size=\"-1\" color=\"#660000\"><b>");
char *Blah2 = strstr(StrText, "</b></font>");
StrText = StrText.Mid(0, (int)Blah1 + 50);
StrText = StrText.Left((int)Blah2 - 1);
return StrText;
}else{
return "Not Found.";
}
}
Well it isnt erroring out now, and I added MessageBoxA to see what Blah1 and Blah2 looked like and it looks good, only problem is that it is still returning StrText in full as it was passed to the function Any reason why ?
P.S. Thanks for the help so far ppl, I'm closer to completing it now
-
August 1st, 2004, 02:28 PM
#7
Hi, leet3r,
Are you checking for 'not found' case in your code?
Thank you.
-
August 1st, 2004, 02:44 PM
#8
if(strstr(StrText, "Yahoo! ID:"))
{
...
}else{
...
}
Do you mean that ?
It's definately a parsing problem because I saved what winsock returns from profiles.yahoo.com/username and its grabbing the right amount of data.
Thanks
-
August 1st, 2004, 03:41 PM
#9
Hi, leet3r,
Do you know the values of the StrText before and after the first call of strstr()? Maybe you have some garbage in it...
Thank you.
-
August 1st, 2004, 06:24 PM
#10
Before. It is the whole profile, example: http://profiles.yahoo.com/birst
After. It returns the whole data minus everything before <font blah blah.
Is there another solution to parsing than this ? I haven't found one single article on parsing text, I heard that there is a parsing example in the microsoft code samples, but I'm not sure it's worth the download if there isnt...
Thanks
-
August 2nd, 2004, 08:26 AM
#11
Guess no-one can answer this, thanks anyway
-
August 2nd, 2004, 11:03 AM
#12
For anyone interested, I found a solution to this problem, it is very simple!
sscanf 
I cant believe the hassle I went through when it was this simple all along..
-
August 2nd, 2004, 11:27 AM
#13
leet3r, before I knew you found sscanf, I quickly put together this code.
It assumes that after "Yahoo! ID:" there is one "<b>" just before the name,
and "</b>" after the name.
It worked for the Birst example.
Code:
CString ID_Parser(CString strText)
{
int nStartIndex, nEndIndex;
if(nStartIndex = strText.Find("Yahoo! ID:",0) != -1)
{
nStartIndex = strText.Find("<b>", nStartIndex);
nEndIndex = strText.Find("</b>", nStartIndex );
strText = strText.Mid(nStartIndex+3, nEndIndex - (nStartIndex+3));
return strText;
}
else
{
return "Not Found.";
}
}
-
August 2nd, 2004, 01:41 PM
#14
Thanks alot! I will use that for parsing things like profiles. sscanf was just for parsing a cookie, that function is much better.
Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|