[Tutor] reading output from a c executable.

Alan Gauld alan.gauld at btinternet.com
Sun Dec 21 02:00:22 CET 2008


"Ravi Kondamuru" <ravikondamuru at gmail.com> wrote

> struct nameid {
>        u32bits len /* total length */
> u32bits id;
> char name; /* name variable length */
> }
>
> As can be seen the length of the name = len - (sizeof(len) + 
> sizeof(id)).
> How do  I use xstruct or struct to unpack such a structure?

I think you will need to read the two fixed length fieldfs then read 
the
name char by char until you hit the null character. After all, thats
what you'd need to do to read it from a file or stream in C too...

Something like:

ltrs = ''
ln = struct.unpack(....)
id = struct.unpack(....)
c = struct.unpack(...)
while c != 0:
    ltrs.append(c)
name = ''.join(ltrs)

You probably could read the two ints and the first char in
one call to struct...

HTH,

Alan G. 




More information about the Tutor mailing list