Save file with extension (SOLVED)

Started by
11 comments, last by microcyb 19 years, 7 months ago
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]
Advertisement
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.
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
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.
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
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.)
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.
Thank you very much for your help
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]
As spudder said:
ofn.lpstrDefExt = "dat";


and BTW, learn to use the [ source ] ... [ /source ] tags.
Kippesoep

This topic is closed to new replies.

Advertisement