Creating a virtual File pointer

D

Dan

Maybe the title is a little misleading, but I don't know a better way
of putting it.

What I would like to do, is create a file pointer that really isn't
pointing to an actual file on disk, but a string in memory. In other
words whatever is being written to the file is actually being written
to a string instead.

Is there any straight forward way of doing this.

The reason that I ask, is the code I want to use this with, is gonna
have to be completely re-written if I want it to write to a string. It
isn't straight forward in writing to the file from one function, but it
is a series of functions that it calls passing the file pointer each
time to where it finally actually writes to it.
 
J

Jack Klein

Maybe the title is a little misleading, but I don't know a better way
of putting it.

What I would like to do, is create a file pointer that really isn't
pointing to an actual file on disk, but a string in memory. In other
words whatever is being written to the file is actually being written
to a string instead.

Is there any straight forward way of doing this.

The reason that I ask, is the code I want to use this with, is gonna
have to be completely re-written if I want it to write to a string. It
isn't straight forward in writing to the file from one function, but it
is a series of functions that it calls passing the file pointer each
time to where it finally actually writes to it.

There is no way in standard C to replace a FILE pointer with anything
else.

Some operating systems have extensions for memory mapped files, which
may or may not do what you want. You need to check with a group that
supports your particular compiler/OS combination to find out if it
provides such extensions and how to use them.
 
E

E. Robert Tisdale

Dan said:
Maybe the title is a little misleading,
but I don't know a better way of putting it.

What I would like to do, is create a file pointer that really isn't
pointing to an actual file on disk, but a string in memory. In other
words whatever is being written to the file is actually being written
to a string instead.

Is there any straight forward way of doing this.

The reason that I ask, is the code I want to use this with, is gonna
have to be completely re-written if I want it to write to a string. It
isn't straight forward in writing to the file from one function, but it
is a series of functions that it calls passing the file pointer each
time to where it finally actually writes to it.

I used Google

http://www.google.com/

to search for

+"ramdisk"

and I found lots of stuff.
 
L

Lawrence Kirby

Maybe the title is a little misleading, but I don't know a better way
of putting it.

What I would like to do, is create a file pointer that really isn't
pointing to an actual file on disk, but a string in memory. In other
words whatever is being written to the file is actually being written
to a string instead.

Is there any straight forward way of doing this.

Unfortunately not, C has no way to direct output through a FILE * to a
string. C has separate functions for writing to string e.h. sprintf()
instead of fprintf() which unfortunately doesn't help in the situation you
describe below.
The reason that I ask, is the code I want to use this with, is gonna
have to be completely re-written if I want it to write to a string. It
isn't straight forward in writing to the file from one function, but it
is a series of functions that it calls passing the file pointer each
time to where it finally actually writes to it.

The simplest and portable approach would be to write the data to a
temporary file and read it back into a string afterwards. This isn't a bad
or necessarily inefficient approach given the file caching in modern OSs.
Standard functions like tmpfile() may be of help here.

There are other possible platform-specific solutions, e.g. pipes, sockets,
memory mapping of files etc. but there's no clear benefit of these if the
amount of data is small. If ther amount of data could be large it is
probably a bad idea to put all of it together in a string.

Lawrence
 
L

Lawrence Kirby

Dan wrote:
....


I used Google

http://www.google.com/

to search for

+"ramdisk"

and I found lots of stuff.

Unfortunately RAM disks just store files in memory, they are a means for
efficiency (at least when alternative storage methods like hard disks are
available). However the OPs question i not about efficiency, it is about
getting data written though a FILE * into a string in the program. RAM
disks provide no help for this problem.

Lawrence
 
G

Giovanni

Dan said:
Maybe the title is a little misleading, but I don't know a better way
of putting it.

What I would like to do, is create a file pointer that really isn't
pointing to an actual file on disk, but a string in memory. In other
words whatever is being written to the file is actually being written
to a string instead.

Is there any straight forward way of doing this.

The reason that I ask, is the code I want to use this with, is gonna
have to be completely re-written if I want it to write to a string. It
isn't straight forward in writing to the file from one function, but it
is a series of functions that it calls passing the file pointer each
time to where it finally actually writes to it.

I had a similar problem in the past and I had to rewrite the functions
building a library that was operating on memory areas rather than on
files. The main drawback is that wasn't possible to mix calls to a
function for operating on a file or operating on memory in the same
program. Or else you have to modify the sources.

Ciao
Giovanni
 
D

Dietmar Schindler

Dan said:
Maybe the title is a little misleading, but I don't know a better way
of putting it.

What I would like to do, is create a file pointer that really isn't
pointing to an actual file on disk, but a string in memory. In other
words whatever is being written to the file is actually being written
to a string instead.

Is there any straight forward way of doing this.

The reason that I ask, is the code I want to use this with, is gonna
have to be completely re-written if I want it to write to a string. It
isn't straight forward in writing to the file from one function, but it
is a series of functions that it calls passing the file pointer each
time to where it finally actually writes to it.

If it is just passing the FILE * through a series of functions, there is
no need to re-write those functions. It would be sufficient to make a
change where the file output function is called. There you could ignore
the FILE * completely and just write to a global string.
Should there be many places where file output functions are called,
however, that may still be much work. If you name your platform and
reveal its FILE structure, it might be possible to suggest another, but
platform-specific solution.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top