简体   繁体   English

从字符串加载C结构

[英]Load C structure from string

My situation is as follows. 我的情况如下。 I obtain FILE * pointer. 我获得了FILE *指针。 I know it can point to FILE type that does not support seek (can be PIPE). 我知道它可以指向不支持搜索的FILE类型(可以是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. 问题是,我的文件包含了一些其他东西,C结构,我需要加载到内存。 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 最有希望似乎是fmemopen,但是当我把它添加到我的代码中时,我得到了

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. 即使我添加了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? 我可以以某种方式在内存中创建FILE *类型的东西,所以我可以调用fread吗? Or is there a way how to read structure from string? 或者有没有办法从字符串中读取结构?

I have used fread as follows: 我使用fread如下:

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

The warning about fmemopen is that the compiler didn't find the prototype, add 关于fmemopen的警告是编译器没有找到原型,添加

#define _GNU_SOURCE

before including all the headers, or, if using GCC, add -D_GNU_SOURCE to the options. 包含所有标头之前 ,或者,如果使用GCC,则将-D_GNU_SOURCE添加到选项中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM