简体   繁体   English

C ++截断iostream类

[英]C++ Truncating an iostream Class

For a project I'm working on for loading/storing data in files, I made the decision to implement the iostream library, because of some of the features it holds over other file io libraries. 对于我正在致力于加载/存储文件中数据的项目,我决定实现iostream库,因为它具有其他文件io库所不具备的某些功能。 One such feature, is the ability to use either the deriving fstream or stringstream classes to allow the loading of data from a file, or an already existent place in memory. 这样的功能之一就是能够使用派生的fstream或stringstream类来允许从文件或内存中已经存在的位置加载数据。 Although, so far, there is one major fallback, and I've been having trouble getting information about it for a while. 尽管到目前为止,有一个主要的后备,而且一段时间以来我一直在获取有关它的信息方面遇到麻烦。

Similar to the functions available in the POSIX library, I was looking for some implementation of the truncate or ftruncate functions. 与POSIX库中可用的功能类似,我正在寻找截断或ftruncate函数的某些实现。 For stringstream, this would be as easy as passing the associated string back to the stream after reconstructing it with a different size. 对于stringstream,这就像将关联的字符串以不同的大小重构之后将其传递回流一样容易。 For fstream, I've yet to find any way of doing this, actually, because I can't even find a way to pull the handle to the file out of this class. 实际上,对于fstream,我还没有找到任何实现此目的的方法,因为我什至没有找到将此类文件的句柄拉出此类的方法。 While giving me a solution to the fstream problem would be great, an even better solution to this problem would have to be iostream friendly. 给我一个解决fstream问题的方法虽然很棒,但要更好地解决此问题,必须是iostream友好的。 Because every usage of either stream class goes through an iostream in my program, it would just be easier to truncate them through the base class, than to have to figure out which class is controlling the stream every time I want to change the overall size. 因为在我的程序中,每种流类的每种用法都经过一个iostream,所以通过基类截断它们会比每次我要更改总体大小时都要弄清楚哪个类在控制流更容易。

Alternatively, if someone could point me to a library that implements all of these features I'm looking for, and is mildly easy to replace iostream with, that would be a great solution as well. 另外,如果有人可以将我指向实现我所寻找的所有这些功能并且可以轻松替换iostream的库,那么这也是一个很好的解决方案。

Edit: For clarification, the iostream classes I'm using are more likely to just be using only the stringstream and fstream classes. 编辑:为澄清起见,我正在使用的iostream类更可能仅使用stringstream和fstream类。 Realistically, only the file itself needs to be truncated to a certain point, I was just looking for a simpler way to handle this, that doesn't require me knowing which type of streambuf the stream was attached to. 实际上,只有文件本身需要被截断到某个点,我只是在寻找一种更简单的方法来处理此问题,不需要我知道流附加到哪种类型的流缓冲。 As the answer suggested, I'll look into a way of using ftruncate alongside an fstream, and just handle the two specific cases, as the end user of my program shouldn't see the stream classes anyways. 正如答案所建议的那样,我将研究一种在fstream旁边使用ftruncate的方法,并且只处理这两种特定情况,因为程序的最终用户无论如何都不会看到流类。

You can't truncate an iostream in place. 您无法在适当位置截断iostream You have to copy the first N bytes from the existing stream to a new one. 您必须将现有流的前N个字节复制到一个新的字节中。 This can be done with the sgetn() and sputn() methods of the underlying streambuf object, which you can obtain by iostream::rdbuf() . 这可以通过基础streambuf对象的sgetn()sputn()方法完成,您可以通过iostream::rdbuf()获得这些对象。

However, that process may be I/O intensive. 但是,该过程可能需要大量I / O。 It may be better to use special cases to manipulate the std::string or call ftruncate as you mentioned. 如前所述,最好使用特殊情况来操纵std::string或调用ftruncate

If you want to be really aggressive, you can create a custom std::streambuf derivative class which keeps a pointer to the preexisting rdbuf object in a stream, and only reads up to a certain point before generating an artifiicial end-of-file. 如果您想真正具有攻击性,则可以创建一个自定义std::streambuf派生类,该类保留指向流中先前存在的rdbuf对象的指针,并且仅在生成人工文件末尾之前读取特定点。 This will look to the user like a shorter I/O sequence, but will not actually free memory or filesystem space. 对用户来说,这看起来像一个较短的I / O序列,但实际上不会释放内存或文件系统空间。 (It's not clear if that is important to you.) (尚不清楚这对您是否重要。)

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

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