简体   繁体   English

使用setvbuf使内存缓冲区的行为类似于FILE *

[英]using setvbuf to make memory buffer act like FILE*

I need a cross platform way of treating memory buffer as FILE* . 我需要一种将内存缓冲区视为FILE*的跨平台方法。 I have seen other questions which point out that there is no portable way to do this (fmemopen in linux is what I need but it fails on Windows platform). 我看到了其他问题,这些问题指出没有可移植的方法来执行此操作(我需要linux中的fmemopen,但在Windows平台上它失败了)。

I have tried using the setvbuf and it seems to work. 我尝试使用setvbuf,它似乎可以工作。 Can anyone please point out the exact problem of using setvbuf function? 任何人都可以指出使用setvbuf函数的确切问题吗?

Also , I have seen the C standard draft WG14/N1256 and 7.19.5.6 says: 另外,我已经看到C标准草案WG14 / N1256和7.19.5.6说:

the contents of array at any time are indeterminate. 数组的内容在任何时候都是不确定的。

I don't understand if I use my own buffer how can its contents be indeterminate? 我不明白如果使用自己的缓冲区,如何确定缓冲区的内容?

EDIT : Thanks for all the answers. 编辑 :感谢所有的答案。 Not using this method anymore. 不再使用此方法。

No really, there's no portable way to do this. 确实没有,没有便携式的方法可以做到这一点。

Using setvbuf may appear to work but you're really invoking undefined behavior, and it will fail in unexpected ways at unexpected times. 使用setvbuf 似乎可以工作,但是您实际上是在调用未定义的行为,并且在意外的时候它将以意外的方式失败。 The GNU C library does have fmemopen(3) as an extension, as you mentioned, but it's not portable to non-GNU systems. 正如您所提到的,GNU C库确实具有fmemopen(3)作为扩展,但是它不能移植到非GNU系统中。

If you're using some library that requires a FILE* pointer and you only have the required data in memory, you'll just have to write it out to a temporary file and pass in a handle to that file. 如果您使用的某个库需要FILE*指针,而内存中只包含所需的数据,则只需将其写到临时文件中,然后将句柄传递给该文件。 Ideally, your library should provide an alternative function that takes a memory pointer instead of a file pointer, but if not, you're out of luck (and you should complain to the library writer about that deficiency). 理想情况下,您的库应该提供一个替代函数,该函数采用内存指针而不是文件指针,但如果不这样做,那么您就不走运了(您应该向库编写者抱怨这种不足)。

Function setvbuf() is used to tell the FILE the memory to be used as buffer, but it does not specify how this memory will be used: that's up to the implementation. setvbuf()函数用于告知FILE用作缓冲区的内存,但未指定如何使用此内存:这取决于实现。

Thus, the contents of the buffer are indeterminate at any time, and if it happens to work for you, it is just by chance. 因此,缓冲区的内容在任何时候都是不确定的,如果碰巧对您有用,那只是偶然。

It depends on what you want to do with the buffer/FILE*. 这取决于您要对buffer / FILE *做些什么。 You can certainly perform simple operations and get away with them, but you cannot guarantee that all of the FILE* operations will perform as expected on your memory buffer. 您当然可以执行简单的操作并摆脱它们,但是您不能保证所有的FILE *操作都能在您的内存缓冲区上按预期执行。

Sorry, there is simply no cross-platform one-liner to get full FILE* characteristics, I've tried myself many times haha 抱歉,根本没有跨平台的一线功能即可获得完整的FILE *特性,我已经尝试了很多次哈哈

what you can try: 您可以尝试的方法:

  • #define-wrapped OS-specific logic #define-wrapped特定于操作系统的逻辑
  • Look further into the interface you are trying to interact with. 进一步查看您要与之交互的界面。 At some point it just plays with a buffer anyway. 无论如何,它只是在玩缓冲。 Then splice in your buffer. 然后在缓冲区中拼接。 This is what I did. 这就是我所做的。
  • Your technique + faith. 您的技术和信念。

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

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