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

查看:51
本文介绍了如何在 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::stringfread() 调整为 std::stringcode> 的 const_cast() 'ed 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::ifstreamrdbuf() 构建一个完全正确、符合标准且可移植的解决方案到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, '');
    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天全站免登陆