Save, NOT save as

This is a discussion on Save, NOT save as within the Windows Programming forums, part of the Platform Specific Boards category; I have these functions for opening and "Save As"ing files, but how would I create a function that, if it ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Exclamation Save, NOT save as

    I have these functions for opening and "Save As"ing files, but how would I create a function that, if it has already been saved, saves it without a dialog box, and if it hasn't been saved, open the save as dialog box? Thanks !


    Code:
    BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName) {
     HANDLE hFile;
     BOOL bSuccess = FALSE;
     hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
     if(hFile != INVALID_HANDLE_VALUE) {
      DWORD dwFileSize;
      dwFileSize = GetFileSize(hFile, NULL);
      if(dwFileSize != 0xFFFFFFFF) {
       LPSTR pszFileText;
       pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
       if(pszFileText != NULL) {
        DWORD dwRead;
        if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)) {
         pszFileText[dwFileSize] = 0;
         if(SetWindowText(hEdit, pszFileText)) {
          bSuccess = TRUE;
         }
        }
        GlobalFree(pszFileText);
       }
      }
      CloseHandle(hFile);
     }
     return bSuccess;
    }
    
    BOOL SaveAsTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName) {
     HANDLE hFile;
     BOOL bSuccess = FALSE;
     hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
     if(hFile != INVALID_HANDLE_VALUE) {
      DWORD dwTextLength;
      dwTextLength = GetWindowTextLength(hEdit);
      if(dwTextLength > 0) {
       LPSTR pszText;
       DWORD dwBufferSize = dwTextLength + 1;
       pszText = GlobalAlloc(GPTR, dwBufferSize);
       if(pszText != NULL) {
        if(GetWindowText(hEdit, pszText, dwBufferSize)) {
         DWORD dwWritten;
         if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) {
         bSuccess = TRUE;
         }
        }
        GlobalFree(pszText);
       }
      }
      CloseHandle(hFile);
     }
     return bSuccess;
    }
    
    // BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName) {} // Heres what i want
    
    void DoFileOpen(HWND hwnd) {
     OPENFILENAME ofn;
     char szFileName[MAX_PATH] = "";
     ZeroMemory(&ofn, sizeof(ofn));
     ofn.lStructSize = sizeof(ofn);
     ofn.hwndOwner = hwnd;
     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0HTML File (*.html)\0*.html\0Cascading Style Sheet (*.css)\0*.css\0XML Document (*.xml)\0*.xml\0XSL StyleSheet (*.xsl)\0*.xsl\0PERL Source Code (*.pl)\0*.pl\0PHP Source Code (*.php)\0*.php\0C Source Code File (*.c)\0*.c\0C++ Source Code File (*.cpp)\0*.cpp\0C/C++ Header File (*.h)\0*.h\0Java Source Code (*.java)\0*.java\0Pascal Source Code (*.pas)\0*.pas\0All Files (*.*)\0*.*\0";
     ofn.lpstrFile = szFileName;
     ofn.nMaxFile = MAX_PATH;
     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
     ofn.lpstrDefExt = "txt";
     if(GetOpenFileName(&ofn)) {
      HWND hEdit = GetDlgItem(hwnd, ID_CHILD_EDIT);
      if(LoadTextFileToEdit(hEdit, szFileName)) {
       SetWindowText(hwnd, szFileName);
      }
     }
    }
    
    // void DoFileSave(HWND hwnd) {} //heres what i want
    
    void DoFileSaveAs(HWND hwnd) {
     OPENFILENAME ofn;
     char szFileName[MAX_PATH] = "";
     ZeroMemory(&ofn, sizeof(ofn));
     ofn.lStructSize = sizeof(ofn);
     ofn.hwndOwner = hwnd;
     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0HTML File (*.html)\0*.html\0Cascading Style Sheet (*.css)\0*.css\0XML Document (*.xml)\0*.xml\0XSL StyleSheet (*.xsl)\0*.xsl\0PERL Source Code (*.pl)\0*.pl\0PHP Source Code (*.php)\0*.php\0C Source Code File (*.c)\0*.c\0C++ Source Code File (*.cpp)\0*.cpp\0C/C++ Header File (*.h)\0*.h\0Java Source Code (*.java)\0*.java\0Pascal Source Code (*.pas)\0*.pas\0All Files (*.*)\0*.*\0";
     ofn.lpstrFile = szFileName;
     ofn.nMaxFile = MAX_PATH;
     ofn.lpstrDefExt = "txt";
     ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
     if(GetSaveFileName(&ofn)) {
      HWND hEdit = GetDlgItem(hwnd, ID_CHILD_EDIT);
      if(SaveAsTextFileFromEdit(hEdit, szFileName)) {
       SetWindowText(hwnd, szFileName);
      }
     }
    }
    Do not make direct eye contact with me.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Code:
    if(_access(file,0)==0)
      SaveWithoutDialog(file);
    else
      SaveWithDialog(file);

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Are those real functions? Or are you using that as an example of what to do? Thanks !
    Do not make direct eye contact with me.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    _access is real, the others are examples.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    What does it do?
    Do not make direct eye contact with me.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    From MSDN:
    Return Value

    Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:

    EACCES

    Access denied: file’s permission setting does not allow specified access.

    ENOENT

    Filename or path not found.

    Parameters

    path

    File or directory path

    mode

    Permission setting

    Remarks

    When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.

    mode Value Checks File For
    00 Existence only
    02 Write permission
    04 Read permission
    06 Read and write permission


    _waccess is a wide-character version of _access; the path argument to _waccess is a wide-character string. _waccess and _access behave identically otherwise.

    Generic-Text Routine Mappings

    TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
    _taccess _access _access _waccess

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 03:42 PM
  2. save and save as functions
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 06-29-2005, 03:49 AM
  3. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 10:21 AM
  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, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21