Jendas
Jendas

Reputation: 3579

Load C structure from string

My situation is as follows. I obtain FILE * pointer. I know it can point to FILE type that does not support seek (can be PIPE). So to make things easier I thought of loading parts of the file to memory as a string buffer.

The problem is, that my file contains, next to some other stuff, C structures, that I need to load to memory. And so far, everything I have tried had failed.

Most promising seemed to be fmemopen, but when I added it to my code I got

warning: implicit declaration of function ‘fmemopen’ [-Wimplicit-function-declaration]
stream = fmemopen (buffer, p_header.bytes, "r");

warning and that is certainly nothing I want. It remained implicit declared even though I added stdio.h include.

Can anything be done about that? Can I somehow create something of FILE * type in memory so I can call fread on it? Or is there a way how to read structure from string?

I have used fread as follows:

fread(&var_of_type_love_struct_t, sizeof(love_struct_t), 1, myfile);  

Upvotes: 0

Views: 847

Answers (1)

Yu Hao
Yu Hao

Reputation: 122433

The warning about fmemopen is that the compiler didn't find the prototype, add

#define _GNU_SOURCE

before including all the headers, or, if using GCC, add -D_GNU_SOURCE to the options.

Upvotes: 3

Related Questions