将文件读入 std::string 的最有效方法是什么?

pho*_*an1 3 c++ string file-io

我目前这样做,最后转换为 std::string 需要 98% 的执行时间。一定会有更好的办法!

std::string
file2string(std::string filename)
{
    std::ifstream file(filename.c_str());
    if(!file.is_open()){
        // If they passed a bad file name, or one we have no read access to,
        // we pass back an empty string.
        return "";
    }
    // find out how much data there is
    file.seekg(0,std::ios::end);
    std::streampos length = file.tellg();
    file.seekg(0,std::ios::beg);
    // Get a vector that size and
    std::vector<char> buf(length);
    // Fill the buffer with the size
    file.read(&buf[0],length);
    file.close();
    // return buffer as string
    std::string s(buf.begin(),buf.end());
    return s;
}
Run Code Online (Sandbox Code Playgroud)

Die*_*ühl 6

作为 C++ 迭代器抽象和算法的忠实粉丝,我希望以下是将文件(或任何其他输入流)读入std::string(然后打印内容)的快速方法:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
    std::string s(std::istreambuf_iterator<char>(std::ifstream("file")
                                                 >> std::skipws),
                  std::istreambuf_iterator<char>());
    std::cout << "file='" << s << "'\n";
}
Run Code Online (Sandbox Code Playgroud)

这对于我自己的 IOStreams 实现来说当然很快,但它需要很多技巧才能真正快速实现。首先,它需要优化算法来处理分段序列:流可以被视为输入缓冲区的序列。我不知道有任何 STL 实现一直在做这个优化。奇怪的用法std::skipws是获取对刚刚创建的流std::istreambuf_iterator<char>的引用:它需要一个临时文件流不会绑定到的引用。

由于这可能不是最快的方法,我倾向于使用std::getline()特定的“换行符”字符,即不在文件中的字符:

std::string s;
// optionally reserve space although I wouldn't be too fuzzed about the
// reallocations because the reads probably dominate the performances
std::getline(std::ifstream("file") >> std::skipws, s, 0);
Run Code Online (Sandbox Code Playgroud)

这假设文件不包含空字符。任何其他角色也可以。不幸的是,std::getline()将 achar_type作为定界参数,而不是int_type成员std::istream::getline()作为定界符所采用的an :在这种情况下,您可以使用eof()从未出现过的字符 ( char_type, int_type, 并eof()引用 的相应成员char_traits<char>)。反过来,成员版本不能使用,因为您需要提前知道文件中有多少字符。

顺便说一句,我看到了一些尝试使用搜索来确定文件大小的尝试。这肯定不会奏效。问题是在std::ifstream(好吧,实际上是在std::filebuf)中完成的代码转换可以创建与文件中的字节数不同的字符数。诚然,当使用默认的 C 语言环境时,情况并非如此,并且可以检测到这不会进行任何转换。否则,流的最佳选择是遍历文件并确定生成的字符数。我实际上认为这是当代码转换可能会很有趣时需要做的事情,尽管我认为它实际上并没有完成。但是,这些示例都没有明确设置 C 语言环境,例如使用std::locale::global(std::locale("C"));. 即使这样,也有必要在std::ios_base::binary模式,否则在读取时行尾序列可能会被单个字符替换。诚然,这只会使结果更短,永远不会更长。

使用从std::streambuf*(即那些涉及rdbuf())提取的其他方法都要求在某个时刻复制结果内容。鉴于文件实际上可能非常大,这可能不是一种选择。然而,如果没有副本,这很可能是最快的方法。为了避免复制,可以创建一个简单的自定义流缓冲区,它接受对 astd::string作为构造函数参数的引用并直接附加到 this std::string

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

class custombuf:
    public std::streambuf
{
public:
    custombuf(std::string& target): target_(target) {
        this->setp(this->buffer_, this->buffer_ + bufsize - 1);
    }

private:
    std::string& target_;
    enum { bufsize = 8192 };
    char buffer_[bufsize];
    int overflow(int c) {
        if (!traits_type::eq_int_type(c, traits_type::eof()))
        {
            *this->pptr() = traits_type::to_char_type(c);
            this->pbump(1);
        }
        this->target_.append(this->pbase(), this->pptr() - this->pbase());
        this->setp(this->buffer_, this->buffer_ + bufsize - 1);
        return traits_type::not_eof(c);
    }
    int sync() { this->overflow(traits_type::eof()); return 0; }
};

int main()
{
    std::string s;
    custombuf   sbuf(s);
    if (std::ostream(&sbuf)
        << std::ifstream("readfile.cpp").rdbuf()
        << std::flush) {
        std::cout << "file='" << s << "'\n";
    }
    else {
        std::cout << "failed to read file\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

At least with a suitably chosen buffer I would expect the version to be the fairly fast. 哪个版本最快肯定取决于系统、所使用的标准 C++ 库,以及许多其他因素,即您想要衡量的性能。


Ker*_* SB 5

你可以试试这个:

#include <fstream>
#include <sstream>
#include <string>

int main()
{
  std::ostringstream oss;
  std::string s;
  std::string filename = get_file_name();

  if (oss << std::ifstream(filename, std::ios::binary).rdbuf())
  {
    s = oss.str();
  }
  else
  {
    // error
  }

  // now s contains your file     
}
Run Code Online (Sandbox Code Playgroud)

oss.str()如果你喜欢,你也可以直接使用;只要确保您在某处进行某种错误检查。

不能保证它是有效的;你可能无法击败<cstdio>fread。正如@Benjamin 指出的那样,字符串流仅通过复制公开数据,因此您可以直接读入目标字符串:

#include <string>
#include <cstdio>

std::FILE * fp = std::fopen("file.bin", "rb");
std::fseek(fp, 0L, SEEK_END);
unsigned int fsize = std::ftell(fp);
std::rewind(fp);

std::string s(fsize, 0);
if (fsize != std::fread(static_cast<void*>(&s[0]), 1, fsize, fp))
{
   // error
}

std::fclose(fp);
Run Code Online (Sandbox Code Playgroud)

(你可能想使用RAII包装FILE*。)


编辑:第二个版本的 fstream 模拟是这样的:

#include <string>
#include <fstream>

std::ifstream infile("file.bin", std::ios::binary);
infile.seekg(0, std::ios::end);
unsigned int fsize = infile.tellg();
infile.seekg(0, std::ios::beg);

std::string s(fsize, 0);

if (!infile.read(&s[0], fsize))
{
   // error
}
Run Code Online (Sandbox Code Playgroud)

编辑:另一个版本,使用流缓冲迭代器:

std::ifstream thefile(filename, std::ios::binary);
std::string s((std::istreambuf_iterator<char>(thefile)), std::istreambuf_iterator<char>());
Run Code Online (Sandbox Code Playgroud)

(注意附加括号以获得正确的解析。)