Writing to memory instead of FILE

A

Anders Thomsen

(Using MS VC++ 6.0)

In a lib file I have a method that takes a FILE as parameter. The
method writes some text to this file. I don't need the text in a file
but in a variable.
So how can I make the method write to a buffer insted of a file on a
disk?

Thanks,
Anders Thomsen
 
J

John Harrison

Anders Thomsen said:
(Using MS VC++ 6.0)

In a lib file I have a method that takes a FILE as parameter. The
method writes some text to this file. I don't need the text in a file
but in a variable.
So how can I make the method write to a buffer insted of a file on a
disk?

Thanks,
Anders Thomsen

You cannot. If you can switch to using C++ I/O where this is REALLY EASY,
instead of using FILE, where it is COMPLETELY IMPOSSIBLE.

john
 
J

Julie

John Harrison wrote:
[snip]
You cannot. If you can switch to using C++ I/O where this is REALLY EASY,
instead of using FILE, where it is COMPLETELY IMPOSSIBLE.

That is a qualified "COMPLETELY IMPOSSIBLE", meaning that within the confines
of proper and portable C++, it isn't possible.

However, there may be ways to accomplish what you desire using operating-system
specific file i/o hooks and/or other hacking & subterfuge -- BUT, these should
only be considered as a last resort, and only by someone that fully understands
the implementation details, implications, and ramifications of such hacking.
Something else that could be considered is how the library is linked in -- if
it is a library in the traditional sense (static link), the OP may be able to
provide specialized implementations of the standard file i/o functions (say,
fputs) and they redirect the output to memory. Depending on how your compiler
performs and resolves the link, this may or may not work. Again, an advanced
(hack) method that has all sorts of caveats.
 
A

Anders Thomsen

You cannot. If you can switch to using C++ I/O where this is REALLY EASY,
instead of using FILE, where it is COMPLETELY IMPOSSIBLE.

I don't have access to the source of the lib file, so I'm afraid
that's not possible.

Thanks for the info.
 
C

Claudio Puviani

Anders Thomsen said:
(Using MS VC++ 6.0)

In a lib file I have a method that takes a FILE as parameter. The
method writes some text to this file. I don't need the text in a file
but in a variable.
So how can I make the method write to a buffer insted of a file on a
disk?

With the caveat that this isn't portable (or standard), you could open both ends
of a pipe or a socket, associate one ebd with a FILE * that you pass to your
library, and populate the variable yourself by reading from the other end. As
convoluted as this is, it would still be much faster than file I/O and it
wouldn't require having write access to the disk.

Claudio Puviani
 
W

Walter Tross

(Using MS VC++ 6.0)

In a lib file I have a method that takes a FILE as parameter. The
method writes some text to this file. I don't need the text in a file
but in a variable.
So how can I make the method write to a buffer insted of a file on a
disk?

How about opening the FILE in "a+" mode, and using ftell and fseek?
 
J

Jesper Madsen

I would try setting up the FILE with setvbuf() , and install a "sufficiently
large buffer" where all file contents fit, and then read the contents from
the buffer...
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top