Solved

parsing a character array

Posted on 2006-06-15
8
243 Views
Last Modified: 2010-04-01
I have the following object:

const u_char* pData;


Inside this object is a bunch of junk. "adfasdfasdf11=asdfasdf"  What is the easiest way to find where the 11= begins in this object.

thanks
0
Comment
Question by:rspiege1
  • 4
  • 4
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16914663
You can use strstr
0
 
LVL 30

Accepted Solution

by:
Axter earned 125 total points
ID: 16914702
#include <string.h>

const u_char* pData = "adfasdfasdf11=asdfasdf";
const char* srchStr = "11";
char *pdest = strstr( pData, srchStr);

if ( pdest != NULL )
{
  int result = (int)(pdest - pData + 1);
  printf( "%s found at position %d\n", srchStr, result );

}
0
 

Author Comment

by:rspiege1
ID: 16914923
It seems to work.  I had to add (Char*) casting for pData in two lines above to get it to compile.  The only thing is it does not return an integer.  instead i get a bunch of garbage.  It may be what I have stored in pData.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:rspiege1
ID: 16914966
Do you know if I could be loosing something then i convert things from a u_char* to a char*?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16914976
>>The only thing is it does not return an integer.

It returns a pointer to the position where the string starts.
You have to use pointer arithmitic to get the index int value, as in above example:
int result = (int)(pdest - pData + 1); //Pointer arithmetic
0
 
LVL 30

Expert Comment

by:Axter
ID: 16914995
>>Do you know if I could be loosing something then i convert things from a u_char* to a char*?

I don't think that's the problem.

Try posting your new code.
0
 

Author Comment

by:rspiege1
ID: 16915152
you solved what I was looking for.  thank you for the help.  My problem is that pData is being filled while using the WinPcap library and it is the way it is reading things.  I'll figure that piece out.

thanks,
Rob
0
 

Author Comment

by:rspiege1
ID: 16915207
actually one more question.  is there a function in c++ to search for a string inside a string.

string temp = "hello world";

how could i search to see if world is in this string?

thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

861 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