Brandon
Brandon

Reputation: 23510

std streams to write to void* and back

How can I get fstream to write to a void pointer?

Basically I've used MapFileView and got a void pointer from it for shared memory.

I've use copymemory to copy a bunch of data back and forth between two separate applications.

Now instead, I want to use the std library to read in a struct via << overload and write it to my mapped memory (Local\MyMemMap, invalid_handle), then read it back via >> overload. It's not a physical file that you can see.

Basically I want to redirect ifstream and ofstream to my void* if possible. Otherwise, what other std streams would allow me to use my pointer and the operator overloads?

I only know of istream, osstream.

Upvotes: 0

Views: 2969

Answers (3)

AProgrammer
AProgrammer

Reputation: 52294

This is the reason for which std::strstream is deprecated since C++98 but has not be removed from the C++11 standard. There is a better alternative for most of its use (std::stringstream) but no standard replacement for the possibility to use a fixed buffer. You don't have to write your own std::streambuf, just use

#include <strstream>

std::strstream memostream(reinterpret_cast<char*>(mem), memsize, reinterpret_cast<char*>(mem));

to have a stream writing to your memory positioned for writing at start. And

std::strstream memistream(reinterpret_cast<char*>(mem), memsize);

for a stream reading at start of it.

Upvotes: 1

jahhaj
jahhaj

Reputation: 3119

It sounds like you want to write a custom stream to read and write from your mapped memory. This web page may help.

Upvotes: -2

jahhaj
jahhaj

Reputation: 3119

operator<< and operator>> are meant for formatted input and output. Sounds like you are reading and writing raw bytes. You should use istream::read and ostream::write for that.

Upvotes: 3

Related Questions