Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bizarre problem with fopen and GetOpenFileName, please help!

96 views
Skip to first unread message

Marin

unread,
Oct 24, 2002, 10:53:03 AM10/24/02
to
All,

This could be an error on my part but if it is then I can't see it.
The code below was run Win2000 if that helps.

I have two functions for reading/writing data from/to a file:

--------------------------------------------
void loadUIInputData()
{
FILE* tempfile;

tempfile=fopen("appdata/uiInputData","rb");
if(tempfile!=null)
{
if(fread(&uiInputData,sizeof(uiInputData),1,tempfile)!=1)uiInputDataFileError(T("read
from"));
if(fclose(tempfile)!=0)uiInputDataFileError(T("close"));
}
}

void saveUIInputData()
{
FILE* tempfile;

tempfile=fopen("appdata/uiInputData","wb");
if(tempfile==null)uiInputDataFileError(T("create"));
else
{
if(fwrite(&uiInputData,sizeof(uiInputData),1,tempfile)!=1)uiInputDataFileError(T("write
to"));
if(fclose(tempfile)!=0)uiInputDataFileError(T("close"));
}
}
--------------------------------------------

Notice that the functions have nothing to do with any GetOpenFileName
function, they use a constant filename and the data being read/written
is simply a structure (which contains other structures - I did that so
that data would be contiguous in memory).

loadUIInputData() is called when the program starts and
saveUIInputData() is called when the program exits. If I run and close
the program it runs fine, no errors, regardless of whether the file
already exists.

I have another function which calls GetOpenFileName:

--------------------------------------------
int selectInputFile()
{
static int returnCode;
static tchar filename[_MAX_PATH+1];

inputFile.lpstrFile=filename;
returnCode=GetOpenFileName(&inputFile);
if(returnCode)SetWindowText(hwndInputFileCombobox,inputFile.lpstrFile);
return returnCode;
}
--------------------------------------------

This is triggered by clicking a "Browse" button. The name of the
selected file is put into a combobox.

Now if I run the program, click browse and select a file the call to
saveUIInputData() fails, i.e. fopen() returns null. What can be
causing this? How can showing a common dialog and setting text in a
combobox cause fopen() to fail? I'm lost as to what the problem is.
Strangely, when the file dialog closes, after selecting a file, there
is a noticable short burst of disk activity. Why? I assume
GetOpenFileName() just sets data in the referenced structure, it
doesn't actually do any disk access per se, right? If anyone has clue
let me know. I desperately need to solve this problem. :(


Many thanks,
Marin

Ed Astle

unread,
Oct 24, 2002, 11:49:27 AM10/24/02
to
Maybe browsing causes the apps default drive/directory to change so that
"appdata/uiInputData" ends up being relative to where ever the browsing took
it ?

Try a getcwd() to see what the current directory is prior to fopen.

Ed.


Joe Hagen

unread,
Oct 24, 2002, 2:45:16 PM10/24/02
to

"Marin" <dol...@psydolphin.net> wrote in message
news:ee1d9799.02102...@posting.google.com...

Marin,

As Ed mentioned, GetOpenFileName actually changes the current
directory.

To work around this, you'd probably have to code something like:

GetCurrentDirectory(sizeof(savedir),savedir);
GetOpenFileName(&ofn);
SetCurrentDirectory(savedir);

to ensure that the current directory is preserved.

Joe


Cris Dunbar

unread,
Oct 24, 2002, 3:00:00 PM10/24/02
to
dol...@psydolphin.net (Marin) wrote in message news:<ee1d9799.02102...@posting.google.com>...

> Now if I run the program, click browse and select a file the call to
> saveUIInputData() fails, i.e. fopen() returns null. What can be
> causing this? How can showing a common dialog and setting text in a
> combobox cause fopen() to fail? I'm lost as to what the problem is.
> Strangely, when the file dialog closes, after selecting a file, there
> is a noticable short burst of disk activity. Why? I assume
> GetOpenFileName() just sets data in the referenced structure, it
> doesn't actually do any disk access per se, right? If anyone has clue
> let me know. I desperately need to solve this problem. :(
>
>
> Many thanks,
> Marin

GetOpenFilename changes current directory to the browsed-to directory
unless you specify OFN_NOCHANGEDIR in the OPENFILENAME options. Since
you're using relative pathnames, they are no longer there.

0 new messages