I need a cross platform way of treating memory buffer as 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).
I have tried using the setvbuf and it seems to work. Can anyone please point out the exact problem of using setvbuf function?
Also , I have seen the C standard draft WG14/N1256 and 7.19.5.6 says:
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
setvbufmay appear to work but you’re really invoking undefined behavior, and it will fail in unexpected ways at unexpected times. The GNU C library does havefmemopen(3)as an extension, as you mentioned, but it’s not portable to non-GNU systems.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. 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).