简体   繁体   English

使用fwrite C ++将压缩数据写入文件

[英]Write compressed data to file using fwrite C++

I have a buffer of size 42131221 bytes (42.1MB) that I used to store some compressed data. 我有一个大小为42131221字节(42.1MB)的缓冲区,该缓冲区用于存储一些压缩数据。 Only the first 20MB actually store the compressed data and I am trying to write this to a file using fwrite: 实际上只有前20MB存储了压缩数据,我正尝试使用fwrite将其写入文件:

fwrite (buffer , WHAT_GOES_HERE, buffer_length, pFile);

The second parameter requires the size of each element to write, but this isn't applicable in this case as I just want to write the whole buffer and since it's compressed, it isn't the case that there exists a size of each element. 第二个参数要求每个元素的大小要写入,但是这不适用于这种情况,因为我只想编写整个缓冲区,并且由于它是压缩的,所以并不是每个元素都存在一个大小。

Any idea on how I can get this to work? 关于如何使它起作用的任何想法?

WHAT_GOES_HERE should be sizeof(type of the buffer). WHAT_GOES_HERE应该是sizeof(缓冲区的类型)。 Also, the buffer_length should be the number of "types" you want to write to the file. 另外,buffer_length应该是您要写入文件的“类型”的数量。 I mention this since it seems you do not want to write the entire buffer but only the for 20MB. 我之所以这样说是因为您似乎不想写整个缓冲区,而只想写20MB的缓冲区。

fwrite works on streams, which are buffered. fwrite适用于已缓冲的流。

write is a lower-level API based on file descriptors. write是基于文件描述符的较低级API。 It doesn't know about buffering. 它不知道缓冲。

Rule of thumb is, 经验法则是

If you want to write a single large buffer, go for write. 如果要写入单个大缓冲区,请进行写入。 You use fwrite if you want to write in smaller chunks. 如果要编写较小的块,则可以使用fwrite。

so, you could go for write here. 因此,您可以在这里写信。

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

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