C getopenfilename 获取文件名

标签 c windows file filenames

我有以下代码,问题是,当我打印文件的完整路径名时,数组中每个字符之间有双空格。

// initialization outside any class in .c code
OPENFILENAME ofn;       // common dialog box structure
char szFile[260];       // buffer for file name
HWND hwnd;              // owner window
HANDLE hf;              // file handle
...
...
// inside a function
initializeOpenFile();
GetOpenFileName(&ofn);


            for(i = 0; i < sizeof(szFile)/sizeof(char);i++){
                fprintf(stderr,"%c", szFile[i]);
                }
        }
}

void initializeOpenFile(){
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = 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 = TEXT("All\0*.*\0Text\0*.TXT\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
}

打印我:

enter image description here

我想使用该 char 数组传递给 openFile 函数:

FILE* fp = fopen( filename, "r" );

最佳答案

看起来它是一个宽字符串,即。一串宽字符。 (引用:wikipedia)

所以 szFile 应该声明:

wchar_t szFile[260];

然后你可以使用 wcstombs() 转换它(我认为!)。

char szPath[260];
wcstombs(szPath, szFile, 260);

szPath 现在应该包含一个“正常”(窄)字符串。

关于C getopenfilename 获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228060/

相关文章:

call_usermodehelper 无法正常工作

windows - 构建后如何在 Windows 上安装 Qt?

c - 在 C 中读取 YUV 图像

c - 在 C 中,被调用函数可以使调用函数返回吗?

c - 如何检查变量是否为 C 中的 "const"限定符类型?

c - 在 C 中读/写浮点类型时如何处理字节顺序差异?

windows - 如何配置 cygwin 本地包目录

c++ - 如何创建不卡住的无限循环?

c++ - 如何在 fork 的帮助下并行搜索文件中的字符串? (GNU Linux/g++)

计算文本文件 C 中的帐户数量