Jem
Jem

Reputation: 2275

Obtaining a FILE * handle without actually creating a file on disk

I need to process some data with a legacy library that I can't modify. My problem is that it requires a plain old FILE handle in order to save its results, and I'm required not to write anything on disk at all.

I understood that there's no standard way to do that, but is it possible using windows API, boost or anything else, to obtain a file handle somewhat pointing to memory ?

I found nowhere a solution for which it's explicitly guarantee that no disc access is (systematically) performed.

Upvotes: 3

Views: 1576

Answers (2)

trompa
trompa

Reputation: 2017

Try with

fmemopen

From How to write to a memory buffer with a FILE*?

tbert's answer:

For anyone else who stumbles upon this thread looking for a correct answer: yes, there is a standards-compliant way to use memory as a FILE descriptor: fmemopen or open_memstream, depending on the semantics you want.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html

http://pubs.opengroup.org/onlinepubs/9699919799/functions/open_memstream.html

Upvotes: 1

Anya Shenanigans
Anya Shenanigans

Reputation: 94869

I believe you can fopen a pipe, using the pipe syntax:

fopen("\\\\.\\pipe\\WritePipe", "w+");

You need to create the pipe using CreateNamedPipe, beforehand, but once you've done that you should be able to use the pipe for processing the data.

You'll probably have to create a thread to read from the pipe to ensure that your app will not hang, but it should work for your needs (not being able to touch the file system)

Upvotes: 4

Related Questions