I have the following data i need to add in the void buffer:
MyStruct somedata; // some struct containing ints or floats etc.
string somestring;
How do i do this?
This is my buffer allocation:
void *buffer = (void *)malloc(datasize);
How do i add first the somedata
into the buffer (, which takes lets say 20 bytes), and then after 20 bytes comes the string which is variable size. I was thinking to read the structs byte by byte and add to buffer, but that feels stupid, there must be some easier way...?
Edit: i want this to equal to: fwrite( struct1 ); fwrite( struct2 ); which are called sequentially, but instead of writing to file, i want to write to a void buffer.
Edit 2: Made it working, heres the code:
char *data = (char *)malloc(datasize);
unsigned int bufferoffset = 0;
for(...){
MyStruct somedata; // some POD struct containing ints or floats etc.
string somestring;
... stuff ...
// add to buffer:
memcpy(data+bufferoffset, &somedata, sizeof(MyStruct));
bufferoffset += sizeof(MyStruct);
memcpy(data+bufferoffset, somestring.c_str(), str_len);
bufferoffset += str_len;
}
Anything to fix?