C++中弹出选择文件夹窗口,选择完之后使用已选择的路径,为什么我这样写不行呢

tututuzxcv 2017-09-12 09:33:33
//DISPLAY 6.2 File I/O with Checks on open
//Reads three numbers from the file infile.dat, sums the numbers,
//and writes the sum to the file outfile.dat.
#include <fstream>
#include <iostream>
#include <cstdlib>
#include<atlstr.h>
#include<stdlib.h>
#include<math.h>
#include<direct.h>
#define NO_WIN32_LEAN_AND_MEAN
#include <shlobj.h>

using namespace std;
CString GetSavePath();
int main()
{
GetSavePath();
CString x;
x=GetSavePath();
cout << x<<endl;
ifstream in_stream;
ofstream out_stream;

in_stream.open("x//.txt");
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

out_stream.open("x//.txt",ios::app);
if (out_stream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}

int first, second, third;
in_stream >> first >> second >> third;
out_stream << "The sum of the first 3\n"
<< "numbers in infile.dat\n"
<< "is " << (first + second + third)
<< endl;

in_stream.close();
out_stream.close();

return 0;
}

CString GetSavePath()
{

CString strpath;

TCHAR Buffer[MAX_PATH];
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = NULL;
bi.ulFlags = BIF_RETURNONLYFSDIRS; //要求返回文件系统的目

bi.ulFlags = BIF_NEWDIALOGSTYLE; //窗口可以调整大小,有新建文件夹按钮

bi.pszDisplayName = Buffer; //此参数如为NULL则不能显示对话框
bi.lpszTitle = _T("请选择文件夹");
bi.lpfn = NULL;


LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if (pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
//取得文件夹路径到Buffer里

strpath.Format(_T("%s"), Buffer);
}
else
{
strpath= _T(""); // 用户点了取消
}

LPMALLOC lpMalloc;
if (FAILED(SHGetMalloc(&lpMalloc)))
strpath = _T("");


//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();

return strpath;
}
...全文
904 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 楼主 tututuzxcv 的回复:
//DISPLAY 6.2 File I/O with Checks on open //Reads three numbers from the file infile.dat, sums the numbers, //and writes the sum to the file outfile.dat. #include <fstream> #include <iostream> #include <cstdlib> #include<atlstr.h> #include<stdlib.h> #include<math.h> #include<direct.h> #define NO_WIN32_LEAN_AND_MEAN #include <shlobj.h> using namespace std; CString GetSavePath(); int main() { GetSavePath(); CString x; x=GetSavePath(); cout << x<<endl; ifstream in_stream; ofstream out_stream; in_stream.open("x//.txt"); if (in_stream.fail()) { cout << "Input file opening failed.\n"; exit(1); } out_stream.open("x//.txt",ios::app); if (out_stream.fail()) { cout << "Output file opening failed.\n"; exit(1); } int first, second, third; in_stream >> first >> second >> third; out_stream << "The sum of the first 3\n" << "numbers in infile.dat\n" << "is " << (first + second + third) << endl; in_stream.close(); out_stream.close(); return 0; } CString GetSavePath() { CString strpath; TCHAR Buffer[MAX_PATH]; BROWSEINFO bi; ZeroMemory(&bi, sizeof(BROWSEINFO)); bi.hwndOwner = NULL; bi.ulFlags = BIF_RETURNONLYFSDIRS; //要求返回文件系统的目 bi.ulFlags = BIF_NEWDIALOGSTYLE; //窗口可以调整大小,有新建文件夹按钮 bi.pszDisplayName = Buffer; //此参数如为NULL则不能显示对话框 bi.lpszTitle = _T("请选择文件夹"); bi.lpfn = NULL; LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框 if (pIDList) { SHGetPathFromIDList(pIDList, Buffer); //取得文件夹路径到Buffer里 strpath.Format(_T("%s"), Buffer); } else { strpath= _T(""); // 用户点了取消 } LPMALLOC lpMalloc; if (FAILED(SHGetMalloc(&lpMalloc))) strpath = _T(""); //释放内存 lpMalloc->Free(pIDList); lpMalloc->Release(); return strpath; }
strpath.Format(_T("%s"), Buffer); 这一句直接用strpath=Buffer试试。 不行了就单步调试。
  • 打赏
  • 举报
回复
用这句才能把文件夹的路径放到buffer里面。 SHGetPathFromIDList(pIDList, Buffer);
赵4老师 2017-09-13
  • 打赏
  • 举报
回复
百度搜相关关键字。

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧