• Advertisement
Sign in to follow this  

File opening troubles

This topic is 3552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, in c++ with visual express 2008, on vista, i seem to be having a little hiccup here. I have a function, which im going to strip down just to show how another function of mine is called, then ill show that function in its entirety, and then explain my hiccup.
void function(std::string filename) {
    if ( CheckFileExistence( filename.c_str() ) ) {
        //what im wanting to have work
    }
    else {
        //what is happening because CheckFileExistence is returning false
    }
}

bool CheckFileExistence(LPCTSTR filename)
{
	fstream fStrm;
	fStrm.open(filename, fstream::in);
	if (fStrm)
	{
		fStrm.close();
		return TRUE;
	}
	else
		return FALSE;
}


OK, now im calling function like so
function("tileset");

OK, now, im running the program from outside the IDE, with the file "tileset" in the same folder as the .exe. Its almost always worked for me before, but i can seem to for the life of me figure out why it refuses to believe the file exists. Now Im assuming because this checking function doesnt see the file, that direct3d wont be able to load it either, even tho i can see the icon, in the folder, with the correct name (ive even copied the name from the files properties into the code to make certain its the same), ive tried with and with the .bmp suffix... is there any solution to figure this out? Im almost at the point of assuming ive borked something big time with my lil direct3d class, and that i should rebuild it, taking even smaller steps than i did before...

Share this post


Link to post
Share on other sites
Advertisement
Never rely on the idea that the CD (Current Directory) is the same as your executable. The first thing I do in my applications is get the folder of my executable (GetModuleFileName can help with this, in .NET you use Application.StartupPath()) Then you need to get the folder from that and combine it with the file you want to load. In .NET it's Path.Combine(). Once you have a fully qualified path to the file then load it.

Share this post


Link to post
Share on other sites
Visual Studio also has an option to set the working directory under the debugging section of the project settings, which can be useful if you want to store the data outside of the debug / release folders.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement