c++ - 如何选择文件保存到的位置?

标签 c++ mfc filedialog

<分区>

我是 C++ 的新手,我希望能够打开文件资源管理器并能够选择要保存到的位置。它目前保存在与 c++ 文件相同的文件夹中。 我该怎么做?谢谢。

std::ofstream testFile;
testFile.open("Test.csv");
testFile << "Test";
testFile.close();

最佳答案

试试这个(虽然没有测试):

void CMyMFCDlg::OnBnClickedButtonBrowseCvsFile()
{
    CFileDialog dlg(TRUE);
    dlg.m_ofn.lpstrFilter = L"cvs files (*.cvs)\0*.cvs\0\0";
    dlg.m_ofn.lpstrInitialDir = L"D:\\MyDefaultDir\\"; //optional line
    dlg.m_ofn.lpstrTitle = L"Open cvs file";

    if (dlg.DoModal() == IDOK)
    {
         CString filenamewithpath = dlg.GetPathName();
         std::ofstream testFile;
         testFile.open(filenamewithpath.GetString());  // unicode
         //testFile.open(CStringA(filenamewithpath).GetString());  //multibyte
         testFile << "Test";
         testFile.close();
    }
}

关于c++ - 如何选择文件保存到的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57409427/

相关文章:

c++ - 通过用于 Objective-C/iOS 的 UIInterfaceOrientationMask 了解 C++ 枚举

c++ - 如何在 C++ 正则表达式中匹配换行符?

c++ - 程序集用寄存器值替换常量

python - 在一个文件类型中添加多个扩展名 mac - tkinter/filedialog/askopenfilename

python - FileDialog 方法的文件类型属性在 Windows 10 上没有文件扩展名

c++ - 使用 cURL 在 C++ 中将网页保存到内存

c++ - 无法从 MFC DLL 创建模式对话框

c++ - AfxGetInstanceHandle() 触发断言失败

c++ - Windows C++ 将启动的子进程带到前台

java - 以 OSX 风格实现保存对话框