如何将文件读入 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()
。这要求 std::string
的数据是连续的,这不是标准要求的,但似乎所有已知实现都是如此。更糟糕的是,如果以文本模式读取文件,则 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
。但是,这可能会复制字符串数据和/或不必要地重新分配内存。
- 所有相关的标准库实现是否足够聪明,可以避免所有不必要的开销?
- 还有另一种方法吗?
- 我是否错过了一些已经提供所需功能的隐藏 Boost 功能?
void slurp(std::string& data, bool is_binary)
原文由 wilbur_m 发布,翻译遵循 CC BY-SA 4.0 许可协议