Thread: Save vs Save As.... OPENFILENAME flags

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Save vs Save As.... OPENFILENAME flags

    Sometimes you don't need to Save As perhaps just wanna save to default/existing file..
    My DoFileSave() function which saves data to a file looks as follows

    Code:
    // File save dialog
    void DoFileSave(HWND hwnd)
    {
    	OPENFILENAME ofn;
    	char szFileName[MAX_PATH] = "";
    
    	ZeroMemory(&ofn, sizeof(ofn));
    
    	ofn.lStructSize = sizeof(OPENFILENAME);
    	ofn.hwndOwner = hwnd;
    	ofn.lpstrFilter = "Binary Files (*.dat)\0*.dat\0All Files (*.*)\0*.*\0";
    	ofn.lpstrFile = szFileName;
    	ofn.nMaxFile = MAX_PATH;
    	ofn.lpstrDefExt = "dat";
    	ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    
    	if(GetSaveFileName(&ofn))
    	{
    		WriteToFile(hwnd);
    	}
    }
    What do i change in Flag (or this function) to prevent Save Save dialog box.. I'm adding both Save As & Save on the menu but do not know how to handle Save...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The File Open/Save Dialog is for asking for a filename to save to.
    If you already have it (eg Save instead of Save As), then you don't need to open the dialog in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    The File Open/Save Dialog is for asking for a filename to save to.
    If you already have it (eg Save instead of Save As), then you don't need to open the dialog in the first place.
    I have Save As, but need to change it to Save...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, as I said, in "Save", you already have the filename to save to, so there's no point in bringing up the open/save file dialog at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You (csonx_p) didn't understand.
    When you select File -> Save from a program, it only runs code to call GetSaveFileName if the file has never been saved and hence has no file name yet. Somewhat like this:
    Code:
    void DoFileSave()
    {
        if (filename != "")
            WriteToDisk(filename);
        else
            DoFileSaveAs();
    }
    
    void DoFileSaveAs()
    {
        filename = MyGetSaveFileName();
        WriteToDisk(filename);
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by iMalc View Post
    You (csonx_p) didn't understand.
    When you select File -> Save from a program, it only runs code to call GetSaveFileName if the file has never been saved and hence has no file name yet. Somewhat like this:
    Code:
    void 
    
    DoFileSave()
    {
        if (filename != "")
            WriteToDisk(filename);
        else
            DoFileSaveAs();
    }
    coOL! That if statement will help! thnx

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by iMalc View Post
    You (csonx_p) didn't understand.
    When you select File -> Save from a program, it only runs code to call GetSaveFileName if the file has never been saved and hence has no file name yet. Somewhat like this:
    Code:
    void DoFileSave()
    {
        if (filename != "")
            WriteToDisk(filename);
        else
            DoFileSaveAs();
    }
    
    void DoFileSaveAs()
    {
        filename = MyGetSaveFileName();
        WriteToDisk(filename);
    }
    Oh! flip, hang-on... filename is pointed by lpstrFile member of OPENFILENAME... So, by calling DoFileSaveAs() at least once, then lpstrFile is initialized... But now this means OPENFILENAME must be global to keep pointing on filename until i quit the program... So from my function ..
    Code:
    // File save dialog
    void DoFileSaveAs(HWND hwnd)
    {
    	OPENFILENAME ofn;
    	char szFileName[MAX_PATH] = "";
    
    	ZeroMemory(&ofn, sizeof(ofn));
    
    	ofn.lStructSize = sizeof(OPENFILENAME);
    	ofn.hwndOwner = hwnd;
    	ofn.lpstrFilter = "Binary Files (*.dat)\0*.dat\0All Files (*.*)\0*.*\0";
    	ofn.lpstrFile = szFileName;
    	ofn.nMaxFile = MAX_PATH;
    	ofn.lpstrDefExt = "dat";
    	ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    
    	if(GetSaveFileName(&ofn))
    	{
    		WriteToFile(hwnd, szFileName);
    	}
    }
    ... i must declare OPENFILENAME ofn; globally rather so that i may use/test for it's members in other functions... correct????

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think iMalc's code was not meant to be take literally, but rather as pseudo code. You may find that it's better to pass a filename as an argument.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Oh! flip, hang-on... filename is pointed by lpstrFile member of OPENFILENAME... So, by calling DoFileSaveAs() at least once, then lpstrFile is initialized... But now this means OPENFILENAME must be global to keep pointing on filename until i quit the program... So from my function ..

    ... i must declare OPENFILENAME ofn; globally rather so that i may use/test for it's members in other functions... correct????
    No, no, no. The filename is imalc's example has nothing to do with the OPENFILENAME struct.
    It's LOGIC. Pseudo code says "if not filename has been saved, then open save as dialog, else just save."
    Can't you implement something like that? It's not exactly difficult.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    I think iMalc's code was not meant to be take literally, but rather as pseudo code. You may find that it's better to pass a filename as an argument.

    --
    Mats
    Yes but to have the previous state of szFileName (filename which was saved), i either have to return it from DoFileSaveAs() or declare't globally ... Anywayz, think am getting both of u's clearly... @Mats, looked at the Double Buffering issue?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Yes but to have the previous state of szFileName (filename which was saved), i either have to return it from DoFileSaveAs() or declare't globally ...
    No, you don't.
    There are several ways around the issue.
    First way is return the ofn struct.
    Second way is define ofn as global.
    Third way is to use a boolean flag to indicate whether or not the user has indeed saved with save as first.
    Fourth way is to use a global filename buffer.

    There are more, of course.
    I'll leave you to figure out which solution might be best.
    And having figured that out, you should be able to understand how to implement save.
    Last edited by Elysia; 05-30-2008 at 04:01 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Remember that you should keep everything as much C++ as you can (avoid char).
    I always thought the cson_x code was standard C rather than C++.

    And no, I haven't looked at the double buffering, since I'm not quite familiar with how do to that - I've only ever dabbled with Windows API functions, and never had to work on re-drawing something via a double-buffering mechanism. Someone who has done this before will have to help here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    I always thought the cson_x code was standard C rather than C++.
    Ehm, you might be right. I think I mixed it up with something else - since I have only seen Windows programming and C, but no confirmation of C++...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Ehm, you might be right. I think I mixed it up with something else - since I have only seen Windows programming and C, but no confirmation of C++...
    On that note... Is C++ = MFC and C = Win API? ... @Elysia, i code in pure C... I get your point on char's... i have in number of cases used _TCHAR instead which i think is c compatible cause it compiled....

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    On that note... Is C++ = MFC and C = Win API? ... @Elysia, i code in pure C... I get your point on char's... i have in number of cases used _TCHAR instead which i think is c compatible cause it compiled....
    You can use C++ without using MFC, but the other way around is not possible, as the MFC itself uses C++.

    _TCHAR is useful to cope with international characters (e.g. you want your application to cope with Japanese, Chinese or Arabic languages).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. save and save as functions
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 06-29-2005, 02:49 AM
  2. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 09:21 AM
  3. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Major glitch losing source files with MSVC++ and WinXP
    By JasonD in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2003, 12:15 PM