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

Reply via email to