當我嘗試讀取大文件(〜412 MB)時,文件突然停止讀取。該計劃工作了一個月,但現在我有錯誤。我不知道爲什麼。你能幫忙嗎?當我檢查它使用淨化它說,該方案已內存分配文件讀取停止
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;
}
}
}
}
我相信該文件是正確的,將是正確的 – user565447 2013-02-28 13:02:28