PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

File Save?

I'm trying to write a file:save routine for my program. After I've already saved, and if I select save again, I want the program to save without opening the save dialog (just save the file). However when I save again, the program shuts down. I was wondering where I was going wrong here.

The problem clearly has something to do with what I'm doing with my "savedtemp" variable.

Here is a snippet of code.

Code:

LRESULT lResult = TRUE;
BOOL saved=FALSE;
TCHAR szFile[MAX_PATH] = TEXT("\0");
WCHAR *savedtemp;
OPENFILENAME ofn;
memset(&(ofn),0,sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;

case ID_FILE_SAVE:
ofn.lpstrFilter = TEXT("Text(*.txt)\0*.txt\0");
ofn.lpstrTitle = TEXT("Save File As");
ofn.Flags = OFN_HIDEREADONLY;
ofn.lpstrDefExt = TEXT("txt);

if(saved==FALSE)
{
if(GetSaveFileName(&ofn))
{
saved=TRUE;
ShowInfo(hwnd,ofn.lpstrFile);

//Add saving file code here
textfile = _tfopen(ofn.lpstrFile,_T("w"));
savedtemp = ofn.lpstrFile;

if(!textfile) return -1;

fprintf(textfile,"Test\n");

fclose(textfile);
}
}
else
{
ShowInfo(hwnd,savedtemp);

//Add saving file code here.
textfile=_tfopen(savedtemp,_T('w"));

if(!textfile) return -1;

fprintf(textfile,"Test\n");

fclose(textfile);

}
break;


Thanks for your help.
GeoffSulcer · 08 Jun 2004, 14:02 UTC
What is the scope of your savedtemp variable?

Geoff
Yeah, at least saved, savedtemp and ofn have to be static or be defined with a larger scope.

Geoff, ever heard of Mantua? That's where I grew up.

Dan East
GeoffSulcer · 08 Jun 2004, 17:00 UTC
Dan,

Sure, Mantua is northeast of where I live now. Working in downtown Akron. How long did you live in the area?

Geoff
Around 20 years, most of it in Mantua. My mom works in Akron.

Dan East
Well, I just tried changing the scope of those variables. It stopped the program from shutting down, but it still won't save over the original file after saving once.

The ShowInfo message box that comes up when it's supposed to save over the original file comes up blank.
Oops, I missed one. szFile needs to have the larger scope as well. savedtmp is completely unnecessary - just use szFile in place of it if saved==TRUE, and completely remove line 27. ofn's scope only needs to be within that function - it no longer needs to be persistent. Overall I think your code is a little more complex than necessary.

I also notice that my script is not properly coloring strings. Arrgh.

Dan East
Does line 41 actually compile?
Code:
_T('w")
Line 41 was a typo. I didn't have access to a cradle where I was, so I had to type in the code into my reply by hand. Sorry about that.