简体   繁体   English

使用fprintf来存储流

[英]Use fprintf to memory stream

I have a vc++ method that uses fprintf to write values to a file in the hard disc. 我有一个vc ++方法,该方法使用fprintf将值写入硬盘中的文件。 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? 有什么方法可以将内存流或无符号char指针传递给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). 您可以使用sprintf或更好的snprintf [用于VC ++的_snprintf / snprintf_s ](如Michael Burr所指出的,并在sprintf链接的“备注”部分中进行了说明)。

And, since it's tagged C++, better yet use std::stringstream . 而且,由于它被标记为C ++,因此最好使用std :: stringstream

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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