-3

I am writing a C++ application, which needs to call a library function that expects FILE* opened for read. I have no code of that function - it a black box to me. The data that I need to pass to the function is in memory in char* buffer of a known size. Ideally, I need to wrap a my buffer in a FILE* structure and pass it in, but I need all the stdio functions that normally works on a FILE* to work on what I pass in, for I don't know which of the functions it calls - definitely fread, possibly fseek too. The code is rather a performance-sensitive one, so I would like to avoid writing the buffer to disk just so that I could create a FILE* from it by fopen. Is there a way to make a FILE* that would allow from my buffer in memory? Something like stringstream in c++? My code needs to be able to run both on windows and linux.

I've seen quite a few questions where people try to write via FILE* to memory. My case is rather a reverse - I need to present the existing buffer as a readable FILE*.

Thanks a lot!

P.S. yes, Using a C string like a FILE* is exactly what I'm asking, somehow I couldn't find it earlier...

Still, if you could suggest a solution on windows, that would be very helpful!

Serge
  • 1,027
  • 14
  • 21
  • 1
    Which language are you using, `C` or `C++`? – Robᵩ Mar 14 '18 at 00:59
  • man fopen to see the api... – Grady Player Mar 14 '18 at 01:00
  • I think the question is similar to this one: https://stackoverflow.com/questions/11381694/using-a-c-string-like-a-file – Victor Padureanu Mar 14 '18 at 01:02
  • 1
    Well, POSIX systems can use [`fmemopen`](https://linux.die.net/man/3/fmemopen), but [it doesn't look like any such equivalent exists for Windows](https://stackoverflow.com/q/12249610/364696). – ShadowRanger Mar 14 '18 at 01:02
  • I don't know of a function in the standard C library that does that, for for POSIX system (linux,mac,bsd) there is [`fmemopen`](http://man7.org/linux/man-pages/man3/fmemopen.3.html) – Pablo Mar 14 '18 at 01:03
  • 4
    Possible duplicate of [Using a C string like a FILE\*](https://stackoverflow.com/questions/11381694/using-a-c-string-like-a-file) – ShadowRanger Mar 14 '18 at 01:03
  • Does Windows has something like [`pipe`](http://man7.org/linux/man-pages/man2/pipe.2.html)? With a pipe and [`fdopen`](https://linux.die.net/man/3/fdopen) you could simulate that on Windows, but you wouldn't be able to do `fseek`. – Pablo Mar 14 '18 at 01:09
  • 1
    If you write a temp file, it shouldn't require writing to (physical) disk. Sane systems mount a tmpfs on `/tmp`, so you're only using (virtual) memory. – Toby Speight Mar 14 '18 at 15:12

1 Answers1

5

Posix requires the fmemopen() function, which does exactly what you ask.

Glibc certainly has it.

Can't say for sure about Windows, but it appears that MinGW includes newlib, which also implements it. If that's any help.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Possibly worth noting that code equivalent to fmemopen() was/is used in some standard libraries to implement [v]s[n]printf()... – Will Crawford Mar 23 '18 at 19:02