打开文件对话框后文件写入不工作

file writing not working after call to open file dialog

本文关键字:文件 工作 对话框      更新时间:2023-10-16

调用以下函数后,我无法写入任何文件,我尝试了c++的fstream和c的fopen有什么问题,请提前帮助感谢我正在使用代码块mingw Windows 7

string openFileDialog(HWND hwnd,char *fileFilter,char *defaultExtension)
{
    string fileName = "";
    OPENFILENAME ofn;
    char szFileName[MAX_PATH] = "";
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = fileFilter;
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = defaultExtension;
    if(GetOpenFileName(&ofn)) {
        fileName.assign(szFileName);
    }
    ZeroMemory(&ofn, sizeof(ofn));
    return fileName;
}

如果您在对话框中更改文件夹,它将更改您的进程的当前文件夹-尝试添加OFN_NOCHANGEDIR标志

尝试CreateFile和WriteFile。

string s = "file.dat";
HANDLE hFile = CreateFile(s.c_str(),       // name of the write
                   GENERIC_WRITE,          // open for writing
                   0,                      // do not share
                   NULL,                   // default security
                   CREATE_ALWAYS,          // Creates a new file, always
                   FILE_ATTRIBUTE_NORMAL,  // normal file
                   NULL);                  // no attr. template
DWORD writesBytes;
bool writeok = WriteFile(hFile, &Current_Doc, sizeof(Current_Doc), &writesBytes, NULL);
CloseHandle(hFile);

类似的问题,我的答案在这里:

打开对话框