> when I read a big file nearly 40M with ifstream, it is terribly slow;
>
> how can I accelerate the reading speed of my program?
See http://www.dinkumware.com/vc_fixes.html.
There's a one-line fix to fstream that should give you a significant
speed up.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
"XueLiang" <z...@ustc.edu> wrote in message
news:#FeC3LXMBHA.1428@tkmsftngp05...
Do you really need "getline" or "read" would be enough.
getline is much slower, because it parses input for "end of line".
Some sample:
#include <fstream>
using std::ios;
int main()
{
const int size = 10000;
char *pFileBuf = new char[size];
std::ifstream fin("bigfile.txt", ios::in|ios::binary);
int n;
while (fin.read(pFileBuf, size), n=fin.gcount())
{
// do whatever you want with data
}
fin.close();
delete [] pFileBuf;
return 0;
}
Mike.
If you really need performance (say in an inner loop), you should
access the streambuf directly.
std::streamsize num;
while ((num = fin.rdbuf()->sgetn(pFilebuf, size)) > 0)
{
//blah with num characters
}
Tom