简体   繁体   中英

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

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 .

如果您希望能够通过指定目标写入时传递的参数在两者之间无缝切换,那么最好使用C ++流而不是旧的C printf调用来进行I / O。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM