0
我想学习C++。我正在将字符文件读入如下的字符数组中:C++:如何快速读取字符文件到char数组中?
#include <iostream>
#include <fstream>
#include<conio.h>
#include <stdint.h>
using namespace std;
int main() {
char c, str[256];
ifstream is;
cout << "Enter the name of an existing text file: ";
cin.get (str,256);
is.open (str);
int32_t fileSize = 0;
if(is.is_open())
{
is.seekg(0, ios::end);
fileSize = is.tellg();
}
cout << "file size is " << fileSize << "\n";
is.close() ;
is.open (str);
char chararray [fileSize] ;
for(int i = 0 ; i < fileSize ; i++)
{
c = is.get();
chararray [i] = c ;
}
for(int i = 0 ; i < fileSize ; i++)
{
cout << chararray [i];
}
is.close();
getch();
return 0;
}
但是,此代码对于读取大型char文件很慢。现在,如何以快速的方式读取char文件到char数组中?在Java中,我通常使用内存映射缓冲区。它也在C++中。对不起,我是C++新手。
内存映射平台(操作系统)的一部分,所以如果你能在Java中做到这一点,那么你可以做的在C和C++。尽管如此,取决于你的平台。另外,“大文件”有多大? –
我很确定C++ _does_提供内存映射文件。 –
请参阅[mmap()](http://linux.die.net/man/2/mmap) – tomahh