PDA

View Full Version : writing in to a string via FILE*



caduel
8th July 2008, 16:03
I have some (C-)code that wants a FILE*.
In this file (pointer) the output I want is written.

I don't want to create files (if I don't have to). I am merely interested in the output produced.

Is it possible to somehow create a FILE* that prints (directly) into some buffer?
(A C equivalent of std::ostringstream, so to speak.)

Obviously I can create a temporary file, pass that to the function, read the contents of the file and the delete it... (which is what I am doing right now). I was just wondering if there was a more elegant (and succinct, faster due to no file IO etc.) way to achieve that.

Thanks
Christoph

HostV
8th July 2008, 17:57
Creating temporary file might made all process very slow.

wysota
8th July 2008, 19:57
There is a function to convert a file descriptor into a FILE pointer, but I don't think you'll find a way to map it to a string. Doing it the other way is easy using mmap but a reverse operation is probably not available.

caduel
12th July 2008, 09:08
thank you.
I'll post here if I should stumble upon a better solution.