将 Fstream 的一部分复制到 Istringstream
经过大量寻找解决方案后,我决定实际寻求一些帮助!
我有一个由多个块组成的文件,其中每个块可能会也可能不会被压缩。每个块之前都有该块的大小以及该块是否被压缩的指示。每个块内都有一个标识该块的字符串。给定一个块名称,我想从文件中获取关联的块。
我的第一个想法是读取压缩标志和块的大小,将块读入 istringstream,如果需要的话解压缩 istringstream,然后搜索块名称。我可以读取压缩标志和块的大小,但我不知道如何将文件数据块读入 istringstream。我看到人们将整个文件读入 istringstream,但我不想这样做,因为每个块可能需要也可能不需要解压缩。
我错过了任何想法或解决方案的链接吗?或者有更好的算法吗?感谢您的任何评论。
After a lot searching for solutions I've decided to actually ask for some help!
I have a file that consists of a number of blocks, where each block may or may not be compressed. Before each block is an indication of the size of the block and whether the block is compressed. Within each block is a string that identifies the block. Given a block name I would like to get the associated block from the file.
My first thought is to read the compression flag and the size of the block, read just the block into an istringstream, decompress the istringstream if needed, then search for the block name. I can read the compression flag and the size of the block, but I'm at a loss as to how to read the block of file data into the istringstream. I see where people have read entire files into a istringstream but I don't want to do that because each block may or may not need to be decompressed.
Any ideas or a link to a solution that I missed? Or is there a better algorithm? Thanks for any comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么需要将数据读入内存流;为什么不直接将其读入数组或 std::vector 中呢?
Why you need to read the data into a memory stream; why not just read it into an array or an
std::vector
?未经测试,但类似这样的东西应该可以工作:
Untested, but something like this ought to work:
我更喜欢流,因为我的解压缩例程使用 C 风格的字符串。
但是,我应该能够将 std::vector 转换为压缩/解压缩例程所需的内容,因此一旦我使阅读部分正常工作,我就会研究它。
所有这一切的一个单独的组件是从块中提取有用的数据。我实际上并没有太仔细地研究过从 istringstream 中读取数据,因此根据情况的不同,我可能希望从 istringstream 的 std::vector 进程中进行处理。
谢谢你们俩。看起来我必须解释一下 Bjarne 关于 pubsetbuf 的说法:)。
I prefer a stream because of my decompression routine, which uses C-style strings.
But, I should be able to cast a std::vector into what's needed by the compression/decompression routines so once I get the reading part working I'll look into it.
A separate component to all this is pulling the useful data from the block. I haven't actually looked too closely into reading from the istringstream so depending on how that goes will probably dictate my desire to process from a std::vector of process from an istringstream.
Thanks to both of you. Looks like I have to interpret what Bjarne says about pubsetbuf :).
顾名思义,流用于流式文本或二进制处理。您对读取块感兴趣,因此将数据放入字符串流对我来说没有多大意义。
一种优雅的方法是使用 std::streambuf 等作为基础来实现您自己的流。然后,您将在输入处读取和缓冲块,处理压缩与否,并在输出处有一个数据流,您可以在其中使用常用运算符将数据正确读取为字符串、整数等。
否则您将陷入使用流的困境read 方法很像将数据读入缓冲区的系统调用。在这种情况下,我建议将数据读入向量中,并且不再通过使用额外的字符串流来减慢速度。
您可以在网上找到实现您自己的流类的示例,例如 gzstream。 《Standard C++ Iostreams and Locales》一书也很有用。
Streams, as the name implies are intended for streamed text or binary processing. You are interested in reading blocks, so getting data into an string stream does't make much sense to me.
An elegant approach would be to implement your own stream, using std::streambuf etc. as base. And then you will read and buffer blocks at the input, handling compression or not, and have a stream of data at the output, where you can properly read data with the usual operators into strings, integers etc.
Otherwise you are stuck using the stream read method much like a system call to read data into a buffer. In this case I'd suggest reading data into a vector and not slowing things anymore by using an additional stringstream.
You can find examples for implementing your own stream class around the net, such as gzstream. The book Standard C++ Iostreams and Locales book is useful too.
使用迭代器。
Use iterators.