GetOpenFileName and ifstream ... Really Odd

Started by
6 comments, last by gunning 18 years, 9 months ago
My problem is that at one point in my application I use GetOpenFileName to get and read an animation file. At a later point in the application, I use ifstream to read a mesh file. They are unrelated. However, the mesh file cannot be opened if GetOpenFileName succeeds - ie., returns success and the user presses OK. If the user presses cancel, the mesh file can be loaded. Also, if GetOpenFileName is not used at all and a string literal is passed into the animation file loading function, the mesh file also successfully opens. Again, I state the problem is very odd and unexpected. But I am not the only one to experience this problem. I found this post on google groups with no responses. My problem is virtually identical. I am using VC++ 2005 Beta 2 with the latest Platform SDK for Server 2003 SP1. I have recompiled this application in VC++ 2003 .NET to see if it was a problem with VC++ Beta, but the problem reoccurred. Any suggestions or help?
....[size="1"]Brent Gunning
Advertisement
How are you calling GetOpenFileName()? It's possible to make a mistake in its call that will cause a bufffer overrun, causing all sorts of strange behaviour.
Any chance of seeing some code? It may help - especially where you pass the returned filename from GetOpenFileName.

arm.
I love your quick responses. Good point. Here is some code.


c_string file = "base/animations/kick.anim";
if (g_openDialog (file, "Load Left-Facing Animation", "Animations (*.anim)\0*.anim\0\0"))
{
// ... Loading animation here ...
}

----
bool g_openDialog (c_string &file, const char *caption, const char *filter)
{
writeDebug ("Displaying open file dialog.");
OPENFILENAMEA ofn;
char fileName [MAX_PATH];
memset (fileName, 0, sizeof (fileName));
memset (&ofn, 0, sizeof (ofn));
ofn.lStructSize = sizeof(ofn);
if (display)
ofn.hwndOwner = display->getWindowHandle ();
else ofn.hwndOwner = 0;
ofn.lpstrFilter = filter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = caption;
if (GetOpenFileNameA (&ofn))
{
file = fileName;
return true;
}
else return false;
}
----

c_string is a custom string class, but that is not the problem. I have tested it with std::string to be sure.
....[size="1"]Brent Gunning
and if it helps, here is the code for when I read the mesh file. The mesh file is in text format, as is the animation file.

std::fstream inFile;
inFile.open (fileName, std::ios_base::in);
if (!inFile.is_open ())
// ^^ this is where it tells me it fails
....[size="1"]Brent Gunning
IIRC, GetOpenFileName will change the current directory, that will mess you up if you're using relative paths.... not sure if that's your problem, tho...
It sounds silly, sorry, but I take it that you have definately checked that the filename returned is a full and valid path?
arm-

The AP fixed it but thanks too!

AP-

Thanks so much. I wish I knew who you were so I could rate you up. Adding OFN_NOCHANGEDIR to the flags was a quick fix to a problem I thought was much deeper. Why would GetOpenFileName change the current directory anyway when it returns a full path name...

I must say though that you guys are the fastest responding forum group I have ever met online. Thanks guys for everything.
....[size="1"]Brent Gunning

This topic is closed to new replies.

Advertisement