DoFileOpenSave? HELP PLEASE!

This is a discussion on DoFileOpenSave? HELP PLEASE! within the Windows Programming forums, part of the Platform Specific Boards category; I have the code: BOOL LoadFile(HWND hEdit, LPSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; hFile = CreateFile(pszFileName, GENERIC_READ, ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question DoFileOpenSave? HELP PLEASE!

    I have the code:

    BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
    {
    HANDLE hFile;
    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, 0, 0);
    if(hFile != INVALID_HANDLE_VALUE)
    {
    DWORD dwFileSize;
    dwFileSize = GetFileSize(hFile, NULL);
    if(dwFileSize != 0xFFFFFFFF)
    {
    LPSTR pszFileText;
    pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
    if(pszFileText != NULL)
    {
    DWORD dwRead;
    if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
    {
    pszFileText[dwFileSize] = 0; // Null terminator
    if(SetWindowText(hEdit, pszFileText))
    bSuccess = TRUE; // It worked!
    }
    GlobalFree(pszFileText);
    }
    }
    CloseHandle(hFile);
    }
    return bSuccess;
    }

    BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
    {
    HANDLE hFile;
    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if(hFile != INVALID_HANDLE_VALUE)
    {
    DWORD dwTextLength;
    dwTextLength = GetWindowTextLength(hEdit);
    if(dwTextLength > 0)// No need to bother if there's no text.
    {
    LPSTR pszText;
    pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
    if(pszText != NULL)
    {
    if(GetWindowText(hEdit, pszText, dwTextLength + 1))
    {
    DWORD dwWritten;
    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
    bSuccess = TRUE;
    }
    GlobalFree(pszText);
    }
    }
    CloseHandle(hFile);
    }
    return bSuccess;
    }

    BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
    {
    OPENFILENAME ofn;
    char szFileName[MAX_PATH];

    ZeroMemory(&ofn, sizeof(ofn));
    szFileName[0] = 0;

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = "HTML (*.htm)\0*.htm\0All Files (*.*)\0*.*\0\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrDefExt = "htm";

    if(bSave)
    {
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
    OFN_OVERWRITEPROMPT;

    if(GetSaveFileName(&ofn))
    {
    if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
    {
    MessageBox(hwnd, "Save file failed.", "Error",
    MB_OK | MB_ICONEXCLAMATION);
    return FALSE;
    }
    }
    }
    else
    {
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    if(GetOpenFileName(&ofn))
    {
    if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
    {
    MessageBox(hwnd, "Load of file failed.", "Error",
    MB_OK | MB_ICONEXCLAMATION);
    return FALSE;
    }
    }
    }
    return TRUE;
    }

    I also have:

    case CM_FILE_OPEN:

    DoFileOpenSave(hwnd, FALSE);

    break;

    When it runs, I do FILE -> OPEN, but it says Load of File Failed. What is the correct code to load and save HTML files?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I tried your code and it worked ok, try stepping through it with a debugger to find out where your LoadFile() function is failing.
    zen

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I think you also need to use

    GlobalFree(GlobalHandle(pzText));

    Do some validation of the free return to see.
    ie
    Code:
    HGLOBAL hMem;
    hMem=GlobalFree(GlobalHandle(pzText));
    if(hMem!=NULL)
    {
        //error (use GetLastError, sprintf and Messagebox)
    }
    else pzText=NULL;
    This is so you can always test the mem == NULL for alloc's ect.

Popular pages Recent additions subscribe to a feed

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