Sign in to follow this  

GetOpenFileName and ifstream ... Really Odd

This topic is 4544 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

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4544 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this