问题
I've narrowed my code down, and I found the source of the problem, it's when I open a file. The file does exists, and I don't get any warning or errors when compiling.
int main(int argc, const char* args[])
{
cout << "Wellcome" << endl;
cout << args[1];
ifstream exists(args[1]);
if(!exists)
{
printf("FILE NOT FOUND");
return 1;
}
exists.close();
ifstream* in;
in->open(args[1],ios::binary|ios::in);
//do stuff
in->close();
return 0;
}
回答1:
You have created a pointer to an ifstream
object, but you never allocated an ifstream
for it to point to. To fix this, consider just stack-allocating it:
ifstream in;
in.open(args[1],ios::binary|ios::in);
//do stuff
in.close();
In general, you usually don't need to dynamically allocate objects unless you want them to outlive the function that created them.
Hope this helps!
来源:https://stackoverflow.com/questions/11039618/ifstream-crashes-program-when-opening-file