【问题标题】:Return / Print path of a selected file返回/打印所选文件的路径
【发布时间】:2021-01-14 17:42:34
【问题描述】:

我正在尝试创建一个打开对话框的函数,让用户选择一个文件,然后将其路径作为 std::string 返回。到目前为止我有这个,但我正在尝试获取它,以便它打印并将路径作为字符串返回。

auto GetUserFile() {
    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name 
    HWND hwnd{};              // owner window
    HANDLE hf;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = (LPWSTR)szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0"; // I think this sets the file types that can be selected
    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);

        std::cout << ofn.lpstrFile; // ?? prints a bunch of numbers and letters
        // return ofn.lpstrFile; // Want to change this to a string
    }
}

我用于this 的 Windows 文档说 lpstrFile 成员“包含路径和文件名”,但我不确定如何将其实际转换为字符串形式,甚至是我自己可以理解的形式。我可能只是使用了错误的转换方法,我尝试将 lpstrfile 转换为 c 字符串和 std::string,但它们也会给出随机数字或引发访问冲突。

【问题讨论】:

    标签: c++ path windows-10


    【解决方案1】:

    我现在觉得自己像个白痴,但是,不相关的细节,这段代码有效:

    #include <iostream>
    #include <windows.h>
    
    OPENFILENAME ofn;
    DWORD error_value;
    
    using namespace std;
    
    bool openFileDialog(TCHAR szFileName[])
    {
        const TCHAR* FilterSpec = "All Files(.)\0*.*\0";
        const TCHAR* Title = "Open";
    
        const TCHAR* myDir = "C:\\c_plus_plus_trial";
    
        TCHAR szFileTitle[MAX_PATH] = { '\0' };
    
        ZeroMemory(&ofn, sizeof(OPENFILENAME));
    
        *szFileName = 0;
    
        /* fill in non-variant fields of OPENFILENAME struct. */
        ofn.lStructSize = sizeof(OPENFILENAME);
    
        ofn.hwndOwner = GetFocus();
        ofn.lpstrFilter = FilterSpec;
        ofn.lpstrCustomFilter = NULL;
        ofn.nMaxCustFilter = 0;
        ofn.nFilterIndex = 0;
        ofn.lpstrFile = szFileName;
        ofn.nMaxFile = MAX_PATH;
        ofn.lpstrInitialDir = myDir; // Initial directory.
        ofn.lpstrFileTitle = szFileTitle;
        ofn.nMaxFileTitle = MAX_PATH;
        ofn.lpstrTitle = Title;
        //ofn.lpstrDefExt = 0; // I've set to null for demonstration
        ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
    
        // false if failed or cancelled
        if (GetOpenFileName(&ofn) == 1)
        {
            cout << "SUCCESS !!! \n";
            return 1;
        }
        else
        {
            cout << "Failure :( \n";
            error_value = CommDlgExtendedError();
            cout << std::hex << error_value;
    
            return 0;
        }
    }
    
    int main()
    {
        TCHAR openedFileName[MAX_PATH];
    
        if (openFileDialog(openedFileName))
            std::cout << "File location\\name: " << openedFileName << "\n";
        else
            std::cout << "User cancelled the operation\n";
    }
    

    取自here,它要求对于Visual Studio 2019,字符集设置为“未设置”

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2021-09-19
      • 2020-03-19
      • 2022-06-11
      • 2016-01-21
      • 1970-01-01
      • 2021-11-01
      • 2022-11-28
      相关资源
      最近更新 更多