say a struct like this:
typedef struct testVertex_s {
char *vert_name; //for test only...
float x;
float y;
float z;
}testvertex_t;
how to write that to a binary file use python ? I want to read it using fread in c;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why must it be binary? Text is trivial, and much simpler to interact with.
If you really want binary, use the struct module. Make sure to define your endianness, and read each field separately in C, do not try to do a single
fread()into a C structure.You could do the packing like so:
This writes the string as a plain 0-terminated string, followed immediately by the floats. The resulting data from the above is
"hello\x00\xc3\xf5H@\xa4p<B\x00\x00\x88A"(represented as a Python string literal).For this case, you’re going to need to dynamically allocate the string of course, which should be obvious.