如何在C ++中将整个文件读入std :: string? [英] How do I read an entire file into a std::string in C++?

查看:122
本文介绍了如何在C ++中将整个文件读入std :: string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文件读入 std :: string ,即一次读取整个文件?

How do I read a file into a std::string, i.e., read the whole file at once?

文本或二进制模式应由调用方指定。该解决方案应符合标准,可移植且高效。它不应不必要地复制字符串的数据,并且应避免在读取字符串时重新分配内存。

Text or binary mode should be specified by the caller. The solution should be standard-compliant, portable and efficient. It should not needlessly copy the string's data, and it should avoid reallocations of memory while reading the string.

一种执行此操作的方法是统计文件大小,调整大小将 std :: string fread()放入 std :: string const_cast< char *>()编辑的 data()。这就要求 std :: string 的数据是连续的,这不是标准要求的,但是对于所有已知的实现都是如此。更糟糕的是,如果以文本模式读取文件,则 std :: string 的大小可能不等于文件的大小。

One way to do this would be to stat the filesize, resize the std::string and fread() into the std::string's const_cast<char*>()'ed data(). This requires the std::string's data to be contiguous which is not required by the standard, but it appears to be the case for all known implementations. What is worse, if the file is read in text mode, the std::string's size may not equal the file's size.

可以使用 std :: ifstream rdbuf()构建完全正确,符合标准的便携式解决方案 code>放入 std :: ostringstream ,然后从其中进入 std :: string 。但是,这可能会复制字符串数据和/或不必要地重新分配内存。

A fully correct, standard-compliant and portable solutions could be constructed using std::ifstream's rdbuf() into a std::ostringstream and from there into a std::string. However, this could copy the string data and/or needlessly reallocate memory.


  • 所有相关的标准库实现是否足够聪明,可以避免所有不必要的开销?

  • 还有另一种方法吗?

  • 我想念一些已经提供所需功能的隐藏Boost功能吗?


void slurp(std::string& data, bool is_binary)


推荐答案

一种方法是将流缓冲区刷新到单独的内存流中,然后将其转换为 std :: string

One way is to flush the stream buffer into a separate memory stream, and then convert that to std::string:

std::string slurp(std::ifstream& in) {
    std::ostringstream sstr;
    sstr << in.rdbuf();
    return sstr.str();
}

这非常简洁。但是,正如问题中指出的那样,此操作执行冗余副本,不幸的是,根本没有办法删除此副本。

This is nicely concise. However, as noted in the question this performs a redundant copy and unfortunately there is fundamentally no way of eliding this copy.

唯一避免冗余副本的实际解决方案是不幸的是,阅读是手动循环进行的。由于C ++现在保证了连续的字符串,因此可以编写以下(≥C++ 14):

The only real solution that avoids redundant copies is to do the reading manually in a loop, unfortunately. Since C++ now has guaranteed contiguous strings, one could write the following (≥C++14):

auto read_file(std::string_view path) -> std::string {
    constexpr auto read_size = std::size_t{4096};
    auto stream = std::ifstream{path.data()};
    stream.exceptions(std::ios_base::badbit);

    auto out = std::string{};
    auto buf = std::string(read_size, '\0');
    while (stream.read(& buf[0], read_size)) {
        out.append(buf, 0, stream.gcount());
    }
    out.append(buf, 0, stream.gcount());
    return out;
}

这篇关于如何在C ++中将整个文件读入std :: string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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