I am trying to make a dialog box that can open, edit and save .txt files. I referred my codes in this tutorial.
How am I going to modify the code so that instead of a "save as" function, it will directly "save" the file that it opened/edited.

Thanks in advance.

When you click "Save as..." the dialog box appears. Dialog Box for choosing the file to save.

For first you have to remove code, that calls the window appearing.
Then you must have variable(pointer) of current file.
After that you can type function, which will save changes into current file.

If you show your code of "Save as..." I can suggest more.

here's the code from the tutorial I mentioned above.

BOOL SaveTextFileFromEdit(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);
        // No need to bother if there's no text.
        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;

I experimented removing some functions, but I still was not able to solve the problem.

Something like this

BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName)
{
    HANDLE hFile;
    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
        TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //clear file
    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit);
        // No need to bother if there's no text.
        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;

So I changed one flag in function CreateFile. I typed TRUNCATE_EXISTING instead of CREATE_ALWAYS. This flag will clear the file, and you will able to save changed information to the same file.

P.S. also you no need to pass file name into this function. Just make global variable of current file name.

P.S. Dont forget to close file before calling this function.

This article has been dead for over six months. Start a new discussion instead.