当我尝试读取大文件(~412 MB)时,文件突然停止读取。该程序运行了一个月,但现在我出错了。我不知道为什么。你能帮忙吗?当我使用 Purify 检查它时,它说程序的内存分配失败
s.write(block, f.gcount());
我正在将一个大文件写入缓冲区(我正在读取大块文件)。然后我解析这个缓冲区并读取文件的下一部分。我试图用更少的文件读取文件,但结果是一样的:程序刚刚停止读取,然后什么也不做。没有任何异常(我试图捕捉std::exception)是什么原因?你能帮忙吗?
该文件非常简单:
p edge 45 45
e 4 1
e 5 6
等等
这是代码:
readFile(char name[]) {
ifstream f;
f.open(name,ifstream::binary);
char buffer[256], token[20];
int i, j, k, tmp;
int vi = 0, vj = 0;
int num_edges = 0;
if (! f.is_open())
{
cout << "Error opening file: " << name << endl;
//_getch();
exit(1);
}
strstream s;
static const int N = 1024*1024;
char block[N];
while (! f.eof() )
{
s.clear();
f.read(block, N);
s.write(block, f.gcount());
while (! s.eof())
{
s.getline(buffer, 250);
if (s.eof())
{
s.write(buffer, s.gcount());
break;
}
if (buffer[0] == 'c')
{
continue;
}
if (buffer[0] == 'p')
{
cout << buffer << endl;
sscanf(&buffer[7], "%d", &globalColouredVertices.size);
if(globalColouredVertices.size > MAX_VERTICES) {
cout << "Too many vertices (> " << MAX_VERTICES << ")"<< endl;
exit(2);
}
//e.resize(globalColouredVertices.size);
for (i = 0; i < globalColouredVertices.size; i++)
{
globalColouredVertices.ele[i].point = i;
}
}
if (buffer[0] == 'e')
{
num_edges++;
i = 2;
j = 0;
while ((buffer[i] >= '0') && (buffer[i] <= '9'))
{
token[j++] = buffer[i];
i++;
}
token[j] = '\0';
vi = atoi(token);
i++;
j = 0;
while ((buffer[i] >= '0') && (buffer[i] <= '9'))
{
token[j++] = buffer[i];
i++;
}
token[j] = '\0';
vj = atoi(token);
vi--;
vj--;
e[vi][vj] = 1;
e[vj][vi] = 1;
}
if (num_edges % 10000 == 0)
cout << num_edges << endl;
else if (num_edges % 24380000 == 0) {
cout << endl;
}
}
}
}