调用打开文件对话框后文件写入不起作用

时间:2013-06-05 07:54:37

标签: c++ c winapi

调用以下函数后我无法写入任何文件我试过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;
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

尝试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);

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

OPENFILENAME open dialog

相关问题