-6

There are many similar questions, but mine is a bit different. Mine involves a Windows GUI. I am using an open file dialog or an "OPENFILENAME". I want to get the dialog result as OK when the user clicks the OK button and then open a text encoded file. I have done it in Java, but the UI looks weird. So I am not sure whether or not people would like it. I want to learn C++ as well, so I need some help. I have a text box called "hWndEdit" in the WM_COMMAND message inside my program. After opening, the text in the file is supposed to be displayed in the textBox I have specified. I have defined the function in a header file with the following code:

#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

void OpenFile()
{
        OPENFILENAME ofn;       // common dialog box structure
        HWND hwnd = nullptr;              // owner window
        HANDLE hf;              // file handle

        // Initialize OPENFILENAME
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = hwnd;
        // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
        // use the contents of szFile to initialize itself.
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = L"NoteRecorder Notes\0(*.recnote)\0Text\0(*.txt)\0";
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrInitialDir = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

        // Display the Open dialog box. 

        if (GetOpenFileName(&ofn) == TRUE)
        {
            hf = CreateFile(ofn.lpstrFile,
                GENERIC_READ,
                0,
                (LPSECURITY_ATTRIBUTES)NULL,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                (HANDLE)NULL);
        }
}

Then I call it from my WM_COMMAND message:

case WM_COMMAND:
switch (wParam)
{
case 12:
OpenFile();
break;
}

But when the user presses the OK button in the OPENFILENAME, it doesn't read any text from the file. How can I accomplish this?

And I want to write files with a save file dialog. Tell me how to do that as well.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 4
    Which part of this code is supposed to read anything from the file? All you are doing is opening it for reading – UnholySheep Sep 08 '21 at 22:27
  • 1
    Maybe you want to use [https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile) – drescherjm Sep 08 '21 at 22:29
  • 3
    I question if you really want to read your text file using windows api functions directly instead of using c++ iostreams. – drescherjm Sep 08 '21 at 22:33
  • 1
    ***And I want to write files with a save file dialog. Tell me how to do that as well.*** That is probably a separate question. Although I suspect once you are able to read, writing is not that much different. – drescherjm Sep 08 '21 at 22:35
  • 1
    Your file dialog code is broken. And you should use C++ standard library to read the file. – David Heffernan Sep 09 '21 at 00:32
  • @DavidHeffernan How do I fix it? – user16709023 Sep 09 '21 at 18:52
  • Read the documentation. But then don't fix it, use IFileDialog instead. As the documentation will tell you. – David Heffernan Sep 09 '21 at 20:07

1 Answers1

2

The main purpose of the functions GetOpenFileName and GetSaveFileName is to provide you with the filename that the user selected. However, doing the actual File Input and Output is a different issue. You can use the standard C/C++ library for that or you can use the Windows API functions, such as CreateFile, ReadFile and WriteFile.

Since you seem to already be using the function CreateFile, it would make sense to call ReadFile after verifying that the function call to CreateFile succeeded (i.e. that it did not return INVALID_HANDLE_VALUE). After reading the data, you can add a terminating null character to the data (you should make sure that the memory buffer is large enough) and then pass it to the SetDlgItemText function (if the text box is in a dialog box) or SetWindowText function.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Now, how do you think I can do that? I would like some extra help please. – user16709023 Sep 09 '21 at 18:50
  • @user16709023: Which part do you have trouble with? Getting the data to the file? Or sending the data to your text box? I have provided links to the documentation of all of the functions that I mentioned. Is there something specific that you don't understand in the documentation? Or don't you have a specific question, because you are completely lost? If it is the latter, then you should probably buy [a good book on C++ programming](https://stackoverflow.com/q/388242/12149471) before attempting to do this. – Andreas Wenzel Sep 09 '21 at 19:33
  • @user16709023: Also, here is a [link to the official Microsoft tutorial on Win32 programming](https://learn.microsoft.com/en-us/windows/win32/learnwin32/learn-to-program-for-windows). If your C++ knowledge is insufficient to follow that tutorial, then you should first buy a book on learning how to program in C++. – Andreas Wenzel Sep 09 '21 at 19:33
  • I dropped this project. I did it in C# and I don't plan to learn Windows Gui in C++ anymore. I decided. NET is the way to go – user16709023 Nov 14 '21 at 20:50