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.
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;}
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.
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.
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();
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: