如何从一个fstream正确地128字节读入一个字符串对象? [英] how can I read exactly 128 bytes from an fstream into a string object?

查看:347
本文介绍了如何从一个fstream正确地128字节读入一个字符串对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个fstream读取128字节到一个字符串对象?

How do I read exactly 128 bytes from an fstream into a string object?

我写了一些代码来读取文件的前128个字节并打印然后最后128个字节的文件并打印。最后一部分工作,因为你可以轻松地迭代到EOF,但是如何从正面得到正好128字节?下面的代码不工作,因为你不能添加128到ifstream迭代器,它不可索引,只有incrementable(看起来)。

I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since you can't add 128 to an ifstream iterator, it's not indexable, only incrementable (it seems).

迭代器和* ++它128次,但是必须有一行简单的方法来做到这一点,对吧?

Sure I could make an iterator and *++ it 128 times, but there must be a single line straightforward way to do it, right?

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char **argv)
{
    std::ifstream ifs ("input.txt",std::ifstream::in | std::ifstream::binary);

    if (ifs.good())
    {
    // read first 128 bytes into a string
        ifs.seekg(0,std::ifstream::beg);
        std::string first128((std::istreambuf_iterator<char>(ifs)),
                             (std::istreambuf_iterator<char>(ifs))+128);

        std::cout << first128 << std::endl;

    // read last 128 bytes into a string
        ifs.seekg(-128,std::ifstream::end);
        std::string last128((std::istreambuf_iterator<char>(ifs)),
                            std::istreambuf_iterator<char>());

        std::cout << last128 << std::endl;

        return 0;
    }

    return 1;
}


推荐答案

,但也许你会很高兴它使用迭代器从缓冲区初始化字符串。

My answer uses an intermediate buffer, but perhaps you will be happy that it uses iterators to initialize the string from the buffer.

std::vector<char> buffer(128); // create a buffer
ifs.read( &buffer[0], buffer.size() ); // read to buffer
std::string first128( buffer.begin(), buffer.end() ); // copy from vector

对我来说,似乎他们有一个太可爱的iostreams 。尝试对流I / O使用迭代器是非常复杂的。

To me it seems like they got a little too cute with the implementation of iostreams. Trying to use iterators for stream I/O is overly complicated.

顺便说一下,我怀疑你正在尝试的实现将覆盖下,做各种中间缓冲(也许在内核中,一些在库中),以及重新分配和复制字符串几次,因为它增长。

By the way, I suspect the implementation that you were attempting will, under the covers, do a variety of intermediate buffering (perhaps some in the kernel, some in the library) as well as re-allocating and copying the string several times as it grows.

另一个想法:你真的需要一个标准字符串的结果吗?你可能只是从向量工作 - 避免了复制到字符串的最后一步。或者,如果你感觉冒险,你可以创建自己的字符串类, 允许你以与向量相同的方式暴露内部缓冲区。

One other idea: Do you really need the result in a standard string? You might just work from the vector -- avoiding the final step of copying to a string. Or, if you are feeling adventurous, you could create your own string class that does allow you to expose the internal buffer in the same way that vector does.

这篇关于如何从一个fstream正确地128字节读入一个字符串对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆