Doesn't write in file when call GetSaveFileName()

This is a discussion on Doesn't write in file when call GetSaveFileName() within the Windows Programming forums, part of the Platform Specific Boards category; Hi everybody I have a program written in C using the library windows.h among others. In one of the windows ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Doesn't write in file when call GetSaveFileName()

    Hi everybody

    I have a program written in C using the library windows.h among others. In one of the windows I have an edit box and 2 buttons, the first one opens the Windows API to select a file, then it places the path into the edit box. The second one reads the path from the edit box and saves it into a file opened with fopen function and writes with fwrite function.

    The problem is: I select the file, the path goes to the edit box, no problem. When I click on the second button, it puts the path into a variable but it doesn't write in the file (although it shows it wrote thru the result of fwrite). The weird thing is, when I don't select a file, but just write something in the edit box, the program writes into the file without program. It seems that, when the Windows API opens up, it messes up with something that prevents me from writing into the file (I know it doesn't make sense... but somehow it's happening, I just can't figure out what is wrong).

    For the sake of simplicity I created a new program only with the objects and functions that are causing this problem, if you guys can take a look, I would be very happy.

    Thank you

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #define ID_ADD          1001
    #define ID_SAVE        1002
    #define ID_EDIT        1003
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CREATE:
            {
                HWND edit = CreateWindowEx(WS_EX_CLIENTEDGE,
                                        "Edit",
                                        "",
                                        WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT,
                                        30, 60, 420, 20,
                                        hwnd,
                                        (HMENU) ID_EDIT,
                                        (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                        NULL);
                HWND add = CreateWindow("Button",
                                        "Add",
                                        WS_CHILD | WS_VISIBLE | ES_LEFT,
                                        30, 280, 60, 18,
                                        hwnd,
                                        (HMENU) ID_ADD,
                                        (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                        NULL);
                HWND save = CreateWindow("Button",
                                        "save",
                                        WS_CHILD | WS_VISIBLE | ES_LEFT,
                                        100, 280, 60, 18,
                                        hwnd,
                                        (HMENU) ID_SAVE,
                                        (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                        NULL);
            }
            break;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case ID_ADD:
                    {
                        OPENFILENAME ofn;
                        char szFileName[MAX_PATH] = "";
    
                        ZeroMemory(&ofn, sizeof(OPENFILENAME));
    
                        ofn.lStructSize = sizeof(OPENFILENAME);
                        ofn.hwndOwner = hwnd;
                        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\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 edit2 = GetDlgItem(hwnd, ID_EDIT);
                            SendMessage(edit2, WM_SETTEXT, 0, (LPARAM)szFileName);
                        }
                    }
                    break;
                    case ID_SAVE:
                    {
                        char abc[255];
                        HWND edit2 = GetDlgItem(hwnd, ID_EDIT);
                        SendMessage(edit2, WM_GETTEXT, 255, (LPARAM)abc);
                        MessageBox(NULL, abc, NULL, MB_OK);
                        FILE * file_config;
                        file_config = fopen("file.conf", "wb+");
                        if (ferror (file_config))MessageBox(NULL, "error 2", NULL, MB_OK);
                        if (file_config == NULL) MessageBox(NULL, "Error opening config file", "Error!", MB_ICONERROR | MB_OK);
       
                        int writeresult = fwrite(abc, 1, strlen(abc), file_config);
                        fclose(file_config);
       
                        if (writeresult != strlen(abc));
                        else
                        {
                            char a[500];
                            sprintf(a,"written -> %d\n",writeresult);
                            MessageBox(NULL, a, NULL, MB_OK);
                        }
                    }
                    break;
                }
            break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style        = 0;
        wc.lpfnWndProc  = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance    = hInstance;
        wc.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
        //wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = "1";
        wc.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            1,
            //WS_EX_CLIENTEDGE,
            "1",
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Problem solved

    This guy "Icon" from Devarticles figured it out.
    The open file dialog was changing the current working directory. Which means 'file.conf' got written to the same directory as the file I chose is in.
    I just added OFN_NOCHANGEDIR to the flags of OPENFILENAME and everything worked fine.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

Tags for this Thread


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