3

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.

Jem
  • 2,255
  • 18
  • 25
  • possible duplicate of [Make a file pointer read/write to an in-memory location](http://stackoverflow.com/questions/5135854/make-a-file-pointer-read-write-to-an-in-memory-location) – Graham Borland Jan 10 '13 at 10:33
  • That possible duplicate only discusses about linux based solution and does not mention windows at all. – Jem Jan 10 '13 at 10:39
  • possible duplicate of http://stackoverflow.com/questions/313111/dev-null-in-windows. Special file name "NUL" seems to be the answer, I have never used it though. – Dariusz Jan 10 '13 at 10:42
  • Wow, no, null is something else. It's sending your data in a black hole. I actually want to get the results back. – Jem Jan 10 '13 at 10:50
  • Exactly how hard is that "no write to disk" requirement? Is paging forbidden? – MSalters Jan 10 '13 at 13:03
  • @MSalters yes if it means hitting the disk each time, I might as well use a temporary file. It's unclear to me if memory mapped files related function are doing disks accesses or not. In the end, the system always decide to use virtual memory if he needs it, but I want to avoid a systematic disk access in practice. – Jem Jan 10 '13 at 13:41
  • I'm concerned by the numerous attempts at closing this question, provided that the so called duplicate is forever dead (answered for a different platform, of zero help for me). I know it's tempting, but please don't abuse your power. – Jem Jan 10 '13 at 13:44
  • @Jem: Paging happens under memory pressure. Unusual but it can be forced. – MSalters Jan 10 '13 at 13:44
  • The **answers** to the duplicate question may not be helpful to you, but the **question** is nevertheless identical. You could try posting a bounty on that question to spur more responses. – Graham Borland Jan 10 '13 at 21:37

2 Answers2

4

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)

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Thanks a lot. I hoped for a simpler solution, but I'll explore this possibility. – Jem Jan 10 '13 at 13:49
1

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

Community
  • 1
  • 1
trompa
  • 1,967
  • 1
  • 18
  • 26
  • Thanks. These functions are not available on windows, but it's still a valid answer for anyone using Linux. – Jem Jan 10 '13 at 11:18