Bruno
Bruno

Reputation: 175

Use fprintf to memory stream

I have a vc++ method that uses fprintf to write values to a file in the hard disc. I want to change this method so that instead of writing the values to disc, I want to return a pointer to the data.

I know in advance the size I have to allocate. Is there any way to pass a memory stream or unsigned char pointer to fprintf?

thanks

Upvotes: 2

Views: 1988

Answers (2)

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9150

You can use sprintf or better yet snprintf [_snprintf/snprintf_s for VC++] (as Michael Burr pointed out and as it's noted in the Remarks section of the sprintf link).

And, since it's tagged C++, better yet use std::stringstream.

Upvotes: 5

T.E.D.
T.E.D.

Reputation: 44814

If you want the ability to seamlessly switch between the two, perhaps via a parameter passed in designating the target to write to, you might be better off doing your I/O using C++ streams rather than the old C printf calls.

Upvotes: 1

Related Questions