Reputation: 30405
I have a simple data file that I want to load, in a C++ program. For weird reasons, it doesn't work:
The snippet:
void World::loadMap(string inFileName) {
ifstream file(inFileName.c_str(), ios::in);
if (file) {
}
else
{
cout<<"Error when loading the file \n";
exit(-1);
}
}
I call the loadMap method like this:
World::Instance()->loadMap("Map.dat");
(World is a singleton class).
How can I find the exact error, by using try-catch or anything else?
Upvotes: 0
Views: 826
Reputation: 30405
Roger Pate's comment:
Using
"./filename"
(the./
is assumed and not even required) is portable across Windows and Linux, the problem could be that the CWD isn't what he thinks it is, though.
Upvotes: 1
Reputation: 264729
The problem is the working directory.
When you specify a relative path for a file it uses the working directory (which may not be the same as the directory where you application is stored on the file system).
Upvotes: 1
Reputation:
By default, a file failing to open (or any other I/O operation) does not raise an exception. You can change this behaviour, but the standard still provides no means of extracting from an exception the exact reason for failure.
Upvotes: 2
Reputation: 888223
Linux filenames are case-sensitive.
Is your file actually named map.dat
?
Also, did you try putting the file in the current directory?
Upvotes: 1