Sign in to follow this  

Save file with extension (SOLVED)

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

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]

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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



Share this post


Link to post
Share on other sites

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