fatal error using .close()

Hey there.

I'm having trouble closing a file of all things. I'm using fstream and .close() to do it. I'm only reading part way into the file, hen I save my position via tellg() and try to close the file. Since it was crashing I put the file.close() into a try block. It still hangs in the try block. So I checked the flags before the close() and only the good bit is on.

The error is on line 94. It's a runtime error. The program crashes and vista catches it then tries to "find a solution"

Reading from the file works fine. I can open the file in any text editor without trouble.

I compiled using mingw g++.

Here is the code.

[code=c++]#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

char* fileName="output.txt";
long last=0;
int hostNum=0,parasiteNum=0;

class Cell
{
public:
float r;
float x;
float y;
float z;
};

Cell* parasites;
Cell* hosts;

int main(int argc, char* argv[])
{
if(argc>1)
{
fileName=argv[1];
}

ifstream fin(fileName,ios::in);

if(!fin.is_open())
{
cout<<"Unable to open file: "<<fileName<<"\n";
return -1;
}

string c;

fin>>c;
hostNum=atoi(c.c_str());
if(hostNum<1){cout<<"invalid information in file, host cells: "<<c<<endl;return 1;}
fin>>c;
parasiteNum=atoi(c.c_str());
if(parasiteNum<1){cout<<"invalid information in file, parasites: "<<c<<endl;return 1;}

hosts=new Cell[hostNum];
parasites=new Cell[parasiteNum];

for(int q=0;q< hostNum+parasiteNum;q++)
{
fin>>c;//index
int i=atoi(c.c_str());
float r,x,y,z;
fin>>c;
r=atof(c.c_str());
fin>>c;
x=atof(c.c_str());
fin>>c;
y=atof(c.c_str());
fin>>c;
z=atof(c.c_str());

if(i<hostNum)//we have a host cell
{
hosts[i].r=r;
hosts[i].x=x;
hosts[i].y=y;
hosts[i].z=z;
}
else//we have a parasite
{
parasites[i-hostNum].r=r;
parasites[i-hostNum].x=x;
parasites[i-hostNum].y=y;
parasites[i-hostNum].z=z;
}
}
last=fin.tellg();

cout<<"closing file\n";

if(fin.rdstate() == ios::failbit)cout<<"Fail bit\n";
if(fin.rdstate() == ios::badbit)cout<<"Bad bit\n";
if(fin.rdstate() == ios::goodbit)cout<<"Good bit\n";
if(fin.rdstate() == ios::eofbit)cout<<"EOF bit\n";

try
{
fin.close();
}
catch(...)
{
cout<<"what?\n";
}

cout<<"read the file!\n";

delete [] hosts;
delete [] parasites;

return 0;
}[/code]

EDIT: updated code as it doesn't affect the error I'm getting. was accessing parasites out of bounds, and forgot to release the memory from hosts and parasites.
Last edited on
Here is the exception vista is giving me, though I don't understand why the try block isn't catching it.

Unhandled exception at 0x774b8915 in a.exe: 0xC0000005: Access violation reading location 0x4aa20ee0.
You say the exception occurs on line 94? cout<<"what?\n";

In any case, buffer overflows, which is what you have here, cannot be caught with exception handling. In fact, no kind of memory error can be caught in this manner.
Ah, my bad. Line 90. fin.close();

Closing the file is causing a buffer overflow??? Is that what you are saying? If so could you please explain? I just don't get it.

EDIT

I had it pointed out on another forum that I was accessing the parasites array out of bounds. I've fixed that, but I'm still getting the same error on fin.close();
Last edited on
Hi,
You should also share the file you are trying to read from. I think if a code bug is there, it might be related to the format of your data. I tried your code on two different architectures (Mac - leopard and an Opteron-CentOS) with g++ and icpc, with the following data file:

2
2
1 0.2 0.3 0.4 0.6
3 0.2 0.3 0.4 0.6
1 1.2 1.3 1.4 1.6
3 1.2 1.3 1.4 1.6

and ... it always get to the end, no problem.
Cheers
Max

Topic archived. No new replies allowed.