Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ parsing problem (http://www.velocityreviews.com/forums/t622757-c-parsing-problem.html)

puzzlecracker 06-28-2008 05:07 PM

C++ parsing problem
 
I have the following string pattern string Name, followed by n number
of strings, followed by 2 integers. Such as

char * needToParse=" Name, str_1, str_2,...,str_n, integer1,
integer2";

parse it so that the following 4 variables contain these values:
char *name=Name;
char * entry= str1+" "+str2+" "+str_n;

int i1=interger1;
int i2=interger2,

Eric Pruneau 06-28-2008 05:51 PM

Re: C++ parsing problem
 

"puzzlecracker" <ironsel2000@gmail.com> a écrit dans le message de news:
84ebdaed-fea3-4b86-a6cd-745f14e529c7...oglegroups.com...
> I have the following string pattern string Name, followed by n number
> of strings, followed by 2 integers. Such as
>
> char * needToParse=" Name, str_1, str_2,...,str_n, integer1,
> integer2";
>
> parse it so that the following 4 variables contain these values:
> char *name=Name;
> char * entry= str1+" "+str2+" "+str_n;
>
> int i1=interger1;
> int i2=interger2,


here a simple way to do that

int main()
{
string parse = "Parse me,str1,str2,str3,4,6";

int beg_pos = 0;
int end_pos = parse.find(',');
vector<string> parsed;
while(end_pos > 0)
{
parsed.push_back(parse.substr(beg_pos, end_pos-beg_pos));
beg_pos = end_pos+1;
end_pos = parse.find(',', beg_pos);
}
return 0;
}

the string is parsed and results are stored in a vector. Ok I didn't convert
the integers but I'm sure you can find a way to do this!

-----------------------

Eric Pruneau



puzzlecracker 06-29-2008 01:49 PM

Re: C++ parsing problem
 
On Jun 28, 1:51 pm, "Eric Pruneau" <eric.prun...@cgocable.ca> wrote:
> "puzzlecracker" <ironsel2...@gmail.com> a écrit dans le message de news:
> 84ebdaed-fea3-4b86-a6cd-745f14e52...@m3g2000hsc.googlegroups.com...
>
> > I have the following string pattern string Name, followed by n number
> > of strings, followed by 2 integers. Such as

>
> > char * needToParse=" Name, str_1, str_2,...,str_n, integer1,
> > integer2";

>
> > parse it so that the following 4 variables contain these values:
> > char *name=Name;
> > char * entry= str1+" "+str2+" "+str_n;

>
> > int i1=interger1;
> > int i2=interger2,

>
> here a simple way to do that
>
> int main()
> {
> string parse = "Parse me,str1,str2,str3,4,6";
>
> int beg_pos = 0;
> int end_pos = parse.find(',');
> vector<string> parsed;
> while(end_pos > 0)
> {
> parsed.push_back(parse.substr(beg_pos, end_pos-beg_pos));
> beg_pos = end_pos+1;
> end_pos = parse.find(',', beg_pos);
> }
> return 0;
>
> }
>
> the string is parsed and results are stored in a vector. Ok I didn't convert
> the integers but I'm sure you can find a way to do this!
>
> -----------------------
>
> Eric Pruneau


I am thinking along the lines of sscanf without skipping spaces?

Eric Pruneau 06-30-2008 12:47 AM

Re: C++ parsing problem
 

> I am thinking along the lines of sscanf without skipping spaces?


And how will you know how many string there is between the name and the
frist integer?


----------------------

Eric Pruneau



puzzlecracker 07-20-2008 04:23 PM

Re: C++ parsing problem
 

> And how will you know how many string there is between the name and the
> frist integer?



You wouldn't know, it's a undetermined number of strings between the
name and the last two integers.



All times are GMT. The time now is 02:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.