1

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

2 回答 2

2

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

于 2013-06-05T20:39:50.853 回答
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 打开对话框

于 2013-06-05T14:08:47.440 回答