I could do the copy and erase bit with the code i have now. streams are so
nice and convenient i was hoping to be able to use it. they are so flexible i
can't believe there is not a way to say a) give me the remainder of the data
or b) turn off the delimiter crap. Speaking of which is there a way to set the
delimiter?
elrod
[EMAIL PROTECTED] wrote:
> On 28 Sep, Mark B. Elrod wrote:
> > since the mailing list does not like us asking for help int he subject
> > line here is my message again...
> >
> > i have a string which has a bunch of numbers i want to read out and then
> > a char buffer as well. the string is of the form:
> >
> > [num fields] [length field 1] [length field 2] ... [length field n -1]
> > [length field n] [buffer]
> >
> > i can easily read the beginning numbers but the data blob is elluding
> > me. for the first part i want to use the delimiting behavior of >> to
> > pull out the individual numbers but for the second part i want to just
> > get the remainder of the stream including whitespace. anyone out there
> > who is knowledgeable about streams? here is my code so far:
>
> Probably not how you want to do this, but it works:
>
> #include <stdio.h>
> #include <string>
>
> typedef unsigned int uint32;
>
> int main(void)
> {
> string data("2 10 17 mark [EMAIL PROTECTED]");
> uint32 numFields, offset = 0;
>
> sscanf(data.c_str(), "%d%n", &numFields, &offset);
> uint32* fieldLength = new uint32[numFields];
>
> for(uint32 i = 0; i < numFields; i++)
> {
> uint32 temp;
>
> sscanf(data.c_str() + offset, " %d %n", &fieldLength[i], &temp);
> printf("field %d: %d\n", i, fieldLength[i]);
> offset += temp;
> }
>
> string copy = data;
>
> copy.erase(0, offset);
> printf("%s\n", copy.c_str());
> }
>
> That gives:
>
> field 0: 10
> field 1: 17
> mark [EMAIL PROTECTED]
>
> Right, no?
>
> --ruaok Freezerburn! All else is only icing. -- Soul Coughing
>
> Robert Kaye -- [EMAIL PROTECTED] http://moon.eorbit.net/~robert