在像Perl这样的脚本语言中,可以一次性将文件读入变量中。
open(FILEHANDLE,$file);
$content=<FILEHANDLE>;
在C++中,最有效的方法是什么?
在像Perl这样的脚本语言中,可以一次性将文件读入变量中。
open(FILEHANDLE,$file);
$content=<FILEHANDLE>;
在C++中,最有效的方法是什么?
像这样:
#include <fstream>
#include <string>
int main(int argc, char** argv)
{
std::ifstream ifs("myfile.txt");
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
return 0;
}
这个语句
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
可以被拆分为
std::string content;
content.assign( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
如果你只想覆盖现有的 std::string 变量的值,这很有用。
std::string::reserve
方法来分配空间。这将加快执行速度。重新分配字符串内存会浪费大量时间。 - Thomas Matthews最高效的方法,但不是 C++ 的方式:
FILE* f = fopen(filename, "r");
// Determine file size
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
char* where = new char[size];
rewind(f);
fread(where, sizeof(char), size, f);
delete[] where;
#
编辑 - 2刚测试了使用std::filebuf
的方法。看起来这可以被称为最好的C++方法,尽管它不完全是一个C++方法,而更像是一个包装器。无论如何,下面是一段代码块,它的速度几乎与普通的C语言相同。
std::ifstream file(filename, std::ios::binary);
std::streambuf* raw_buffer = file.rdbuf();
char* block = new char[size];
raw_buffer->sgetn(block, size);
delete[] block;
我进行了一个快速的基准测试,结果如下。测试是在读取一个65536K二进制文件时进行的,使用适当的(std::ios:binary
和rb
)模式。
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from IO
[ RUN ] IO.C_Kotti
[ OK ] IO.C_Kotti (78 ms)
[ RUN ] IO.CPP_Nikko
[ OK ] IO.CPP_Nikko (106 ms)
[ RUN ] IO.CPP_Beckmann
[ OK ] IO.CPP_Beckmann (1891 ms)
[ RUN ] IO.CPP_Neil
[ OK ] IO.CPP_Neil (234 ms)
[----------] 4 tests from IO (2309 ms total)
[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (2309 ms total)
[ PASSED ] 4 tests.
iostream
功能和原始C文件读取速度的最佳方式。 - M. Williams最有效的方法是创建一个正确大小的缓冲区,然后将文件读入缓冲区。
#include <fstream>
#include <vector>
int main()
{
std::ifstream file("Plop");
if (file)
{
/*
* Get the size of the file
*/
file.seekg(0,std::ios::end);
std::streampos length = file.tellg();
file.seekg(0,std::ios::beg);
/*
* Use a vector as the buffer.
* It is exception safe and will be tidied up correctly.
* This constructor creates a buffer of the correct length.
* Because char is a POD data type it is not initialized.
*
* Then read the whole file into the buffer.
*/
std::vector<char> buffer(length);
file.read(&buffer[0],length);
}
}
tellg
的结果很可能与可读取的字符数不匹配。 - M.M\0
。#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream f(FILENAME, fstream::in );
string s;
getline( f, s, '\0');
cout << s << endl;
f.close();
}
这取决于很多因素,比如文件大小、文件类型(文本/二进制)等。之前我对下面的函数进行了基准测试,并与使用streambuf迭代器的版本进行了比较——速度大约快了两倍:
unsigned int FileRead( std::istream & is, std::vector <char> & buff ) {
is.read( &buff[0], buff.size() );
return is.gcount();
}
void FileRead( std::ifstream & ifs, string & s ) {
const unsigned int BUFSIZE = 64 * 1024; // reasoable sized buffer
std::vector <char> buffer( BUFSIZE );
while( unsigned int n = FileRead( ifs, buffer ) ) {
s.append( &buffer[0], n );
}
}
#include<iostream>
#include<vector>
#include<iterator>
main(int argc,char *argv[]){
// read standard input into vector:
std::vector<char>v(std::istream_iterator<char>(std::cin),
std::istream_iterator<char>());
std::cout << "read " << v.size() << "chars\n";
}
ifstream file("file", ios::binary);
string fileStr;
istreambuf_iterator<char> inputIt(file), emptyInputIt
back_insert_iterator<string> stringInsert(fileStr);
copy(inputIt, emptyInputIt, stringInsert);