How to create a file in memory ?

L

Lokicer

Hi all,

i want to create a file in memory and access the memory block like a
file(i/o function) in windows 2000. Any hlep are appreciated.

Regards,
Zheng
 
V

Victor Bazarov

Lokicer said:
i want to create a file in memory and access the memory block like a
file(i/o function) in windows 2000. Any hlep are appreciated.

The simplest way I know is to use std::stringstream.

Victor
 
R

Ron Natalie

Lokicer said:
Hi all,

i want to create a file in memory and access the memory block like a
file(i/o function) in windows 2000. Any hlep are appreciated.
Try a Microsoft newsgroup or read the documentation of "CreateFile" in
the MSDN documentations.
 
J

Jerry Coffin

Lokicer said:
Hi all,

i want to create a file in memory and access the memory block like a
file(i/o function) in windows 2000. Any hlep are appreciated.

Sounds a lot like a stringstream.
 
J

Jonathan Turkanis

Victor Bazarov said:
The simplest way I know is to use std::stringstream.

stringstream is useful a lot of the time, but it has the following
limitations (both reasonable, in context):

1. You can't specify that the stream access a pre-existing block of
memory,
2. When you're done performing i/o you can't get direct access to the
underlying memory -- you have to settle for a copy.

A library up for review at boost (by Darlye Walker) offers a
collection of streams and stream buffers for accessing arrays
directly, with this usage:

char buf[1000];
boost::io::pointerstream ps(buf, buf + 1000);

Here buf need not be on the stack -- it could just as well be
dynamically allocated. Of course, unlike with a std::stringstream, the
user has to be careful not to write past the end of the array.

I've submitted a slightly more general iostreams libray which allows
the creation of a pointerstream as follows:

typedef boost::io::stream_facade<array_resource>
pointerstream;
char buf[1000];
pointerstream ps(buf, buf + 1000);

You can also access mmap'd files as follows:

typedef boost::io::stream_facade<mapped_file_resource>
mapped_fstream;
mapped_fstream ms("hello.txt");

Docs are here:

more_io (Daryle Walker) http://tinyurl.com/5d9xt
iostreams (Jonathan Turkanis) http://tinyurl.com/4zfmt

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top