Skip to main content
GameDev.net gamedev.net
🔒 Locked

Save file with extension (SOLVED)

Started by microcyb Sep 17, 2004 at 3:33 PM 11 replies 3.5k views
Original Post
microcyb
microcyb
Have a little C+ question here. Trying to finish my save feature in my ocde, but when I do the save as, it will only store the filename without the .dat at the end. So if I do a save as for a new filename like "dog", I would get just dog, and not dog.dat. Any clues on how this in done? [Edited by - microcyb on September 20, 2004 11:19:39 AM]
pi_equals_3
pi_equals_3
Have you tried making the filename "dog.dat" instead of just "dog"?
If the output stream were to automatically append ".dat" to all files, programmers would be unable to save .txt files for instance.
graveyard filla
graveyard filla
pi is right. you simply just save the file with the extension you want. you could open the file stream as "file.whateverthehellyouwant"
FTA, my 2D futuristic action MMORPG
microcyb
microcyb
Sorry,
In my code I have the menu do a save as, and then I get the win32 save dialog screen.
Enter a name like dog and click save.

What I am trying to understand is how can you auto append the extention .dat if the user does not enter that in.
graveyard filla
graveyard filla
im assuming your using C++, and using std::strings. if your using C++, you should be using std::strings, anyway.


string file_name = Get_Save_File_Name();

file_name += ".dat"


thats all. you can do some checking, too, to see if ".dat" already exist's in the string. im not sure how exactly, but i know theres some sort of "find first occurence of" function. you could even just rawly index into the string, like

if(file_name[file_name.size()-1] == 't' && file_name[file_name.size()-2] == 'a' && file_name[file_name.size()-3] == 'd')
{
THIS ENDS WITH DAT ALREADY!!!
}

thats pretty ugly, but should work. i would find the proper function to do it though.
FTA, my 2D futuristic action MMORPG
Zahlman
Zahlman
I assume he's looking for something specific to the Win32 save dialog.

As it stands now, does your save dialog include a "Save as type" drop-down list? If no, check the MSDN reference to figure out how to put it in. If yes, check the MSDN reference to figure out how to get the selected item from that box, and adjust the file name accordingly. (It's perfectly reasonable to have only one option like ".dat file" in the box).

OTOH, does your program really require the filename to end in anything specific? Why not let the user save as "all files"? (One possible reason is so that you can associate an icon with your file extension. I can't really think of anything else, though.)
Spudder
Spudder
Set the lpstrDefExt field of your OPENFILENAME structure to "dat", this should ensure all filenames entered will have .dat appended to them when the file is written. You'll probably want to set the lpstrFilter field to "DAT Files (*.dat)\0*.dat\0" to only display files with the .dat extension as well.
microcyb
microcyb
Thank you very much for your help
microcyb
microcyb
ok this saves, but again still cannot get that .dat to add to it.

int A_SaveFile( HWND hWnd, BOOL *bModified, TCHAR *szFilePath, BOOL *bUnicode ){	//	// A_SaveFile	// Prompts user to select location to write file	//	OPENFILENAME ofn         = {0};	TCHAR szFile[MAX_PATH+1] = {0};	static TCHAR *szFilter   = 							  T("dat Files\0*.dat\0")							  T("All Files\0*.*\0\0");	if( !bModified || !szFilePath )		return FALSE;		//	// Fill out data structure	//	ofn.Flags             = 0;	ofn.hInstance         = GetModuleHandle( NULL );	ofn.hwndOwner         = hWnd;	ofn.lpstrCustomFilter = 0;	ofn.lpstrFile         = szFile;	ofn.lpstrFileTitle    = 0;	ofn.lpstrFilter       = szFilter;	ofn.lpstrInitialDir   = 0;	ofn.lpstrTitle        = T("DRdat- Save As");	ofn.lStructSize       = sizeof( OPENFILENAME );	ofn.nMaxFile          = MAX_PATH;	//	// Show dialog	//	if( GetSaveFileName( &ofn ) == 0 )		return -1;	//	// User chose file name	//	if( GetFileAttributes( ofn.lpstrFile ) != 0xFFFFFFFF )	{		// File exists, prompt to overwrite		switch( PromptToOverwrite( ofn.lpstrFile ) )		{			case IDCANCEL: 			case IDNO:     return TRUE;		}	}	//	// File doesn't exist, just write it	//	if( !A_WriteFile( hWnd, ofn.lpstrFile, bModified, ofn.nFilterIndex == 0 ? TRUE : FALSE, bUnicode ) )		return FALSE;	SetNewFileName( ofn.lpstrFile, szFilePath);	UpdateUI( hWnd );	return TRUE;}

Edit Opps added source tags

[Edited by - microcyb on September 20, 2004 9:27:44 AM]
Kippesoep
Kippesoep
As spudder said:
ofn.lpstrDefExt = "dat";


and BTW, learn to use the [ source ] ... [ /source ] tags.
Kippesoep
microcyb
microcyb
OK added in ofn.lpstrDefExt = T("dat");
But now how in the heck do I assign it to the
SetNewFileName( ofn.lpstrFile, szFilePath);

int testpad_SaveFile( HWND hWnd, BOOL *bModified, TCHAR *szFilePath, BOOL *bUnicode ){	//	// testpad_SaveFile	// Prompts user to select location to write file	//	OPENFILENAME ofn         = {0};	TCHAR szFile[MAX_PATH+1] = {0};	static TCHAR *szFilter   = 		T("dat Files\0*.dat  \0")		T("All Files\0*.*\0\0");	if( !bModified || !szFilePath )		return FALSE;		//	// Fill out data structure	//	ofn.Flags             = 0;	ofn.hInstance         = GetModuleHandle( NULL );	ofn.hwndOwner         = hWnd;	ofn.lpstrCustomFilter = 0;	ofn.lpstrFile         = szFile;	ofn.lpstrDefExt       = T("dat");	ofn.lpstrFileTitle    = 0;	ofn.lpstrFilter       = szFilter;	ofn.lpstrInitialDir   = 0;	ofn.lpstrTitle        = T(".dat   - Save As");	ofn.lStructSize       = sizeof( OPENFILENAME );	ofn.nMaxFile          = MAX_PATH;	//	// Show dialog	//	if( GetSaveFileName( &ofn ) == 0 )		return -1;	//	// User chose file name	//	if( GetFileAttributes( ofn.lpstrFile ) != 0xFFFFFFFF )	{		// File exists, prompt to overwrite		switch( PromptToOverwrite( ofn.lpstrFile ) )		{			case IDCANCEL: 			case IDNO:     return TRUE;		}	}	//	// File doesn't exist, just write it	//	if( !testpad_WriteFile( hWnd, ofn.lpstrFile, bModified, ofn.nFilterIndex == 0 ? TRUE : FALSE, bUnicode ) )		return FALSE;	SetNewFileName( ofn.lpstrFile, szFilePath);	UpdateUI( hWnd );	return TRUE;}
microcyb
microcyb
If I do this ofn.lpstrDefExt = "dat";

I get this error:
files.cpp(363): error C2440: '=' : cannot convert from 'const char [4]' to 'LPCWSTR'
microcyb
microcyb
Oh now this was dumb.
You have to have it say.
ofn.lpstrDefExt = TEXT ("dat");
not
ofn.lpstrDefExt = "dat";

Thanks for you help folks.
It works now!!!!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.