MFC中获取多个文件路径的方法,获取的多文件路径无法读取doc->LoadFile()值为非
TCHARstrFileter2[]="BIN文件(*.BIN)|*.BIN|AllFiles(*.*)|*.*||";CFileDialogFileDlg(TRUE,"...
TCHAR strFileter2[]="BIN文件(*.BIN)|*.BIN|All Files (*.*)|*.*||";
CFileDialog FileDlg(TRUE,"Bin",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strFileter2,this);
if (FileDlg.DoModal() == IDOK)
{
UpdateData(TRUE);
m_strBinFile2=FileDlg.GetPathName();
UpdateData(FALSE);
}
TiXmlDocument *doc2 = new TiXmlDocument( m_strBinFile2);
if (!doc2->LoadFile())
{
return;
}
打开单个文件doc2->LoadFile()成功。
但是多个文件:
CFileDialog dlgFile(TRUE);
CString m_FilePathName[100] = {0};
//打开CFileDialog对象
dlgFile.m_ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_HIDEREADONLY;
dlgFile.m_ofn.lpstrFilter = _T("Bin Files(*.bin)\0*.bin\0All Files(*.*)\0*.*\0\0");
const int nMaxFiles = 1000;
const int nMaxPathBuffer = (nMaxFiles * (MAX_PATH + 1)) + 1;
LPSTR pc = (LPSTR)malloc(nMaxPathBuffer * sizeof(WCHAR));
if( pc )
{
dlgFile.GetOFN().lpstrFile = pc;
dlgFile.GetOFN().lpstrFile[0] = NULL;
dlgFile.m_ofn.nMaxFile = nMaxPathBuffer;
if (dlgFile.DoModal() != IDOK)
{
return;
}
POSITION pos = dlgFile.GetStartPosition();//获取第一个文件位置
m_FilePathName[i] = dlgFile.GetNextPathName(pos);
CFile myFile(m_FilePathName[i],CFile::modeRead);
TiXmlDocument *doc1 = new TiXmlDocument("D:\\sumA.bin"); //这样可以读取
TiXmlDocument *doc1 = new TiXmlDocument(m_FilePathName[0]);//这样就不能读取
CString str;str.Format(_T("%s"),m_FilePathName[0]);AfxMessageBox(str);
doc1->LoadFile() ;不能读取的
这样的话,获取的路径不能打开,求解为何不能打开,我把单文件和多文件获得的路径都打印出来了,是一样的,但是用数组存的就不能读取文件? 展开
CFileDialog FileDlg(TRUE,"Bin",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strFileter2,this);
if (FileDlg.DoModal() == IDOK)
{
UpdateData(TRUE);
m_strBinFile2=FileDlg.GetPathName();
UpdateData(FALSE);
}
TiXmlDocument *doc2 = new TiXmlDocument( m_strBinFile2);
if (!doc2->LoadFile())
{
return;
}
打开单个文件doc2->LoadFile()成功。
但是多个文件:
CFileDialog dlgFile(TRUE);
CString m_FilePathName[100] = {0};
//打开CFileDialog对象
dlgFile.m_ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_HIDEREADONLY;
dlgFile.m_ofn.lpstrFilter = _T("Bin Files(*.bin)\0*.bin\0All Files(*.*)\0*.*\0\0");
const int nMaxFiles = 1000;
const int nMaxPathBuffer = (nMaxFiles * (MAX_PATH + 1)) + 1;
LPSTR pc = (LPSTR)malloc(nMaxPathBuffer * sizeof(WCHAR));
if( pc )
{
dlgFile.GetOFN().lpstrFile = pc;
dlgFile.GetOFN().lpstrFile[0] = NULL;
dlgFile.m_ofn.nMaxFile = nMaxPathBuffer;
if (dlgFile.DoModal() != IDOK)
{
return;
}
POSITION pos = dlgFile.GetStartPosition();//获取第一个文件位置
m_FilePathName[i] = dlgFile.GetNextPathName(pos);
CFile myFile(m_FilePathName[i],CFile::modeRead);
TiXmlDocument *doc1 = new TiXmlDocument("D:\\sumA.bin"); //这样可以读取
TiXmlDocument *doc1 = new TiXmlDocument(m_FilePathName[0]);//这样就不能读取
CString str;str.Format(_T("%s"),m_FilePathName[0]);AfxMessageBox(str);
doc1->LoadFile() ;不能读取的
这样的话,获取的路径不能打开,求解为何不能打开,我把单文件和多文件获得的路径都打印出来了,是一样的,但是用数组存的就不能读取文件? 展开
展开全部
你好 亲
修改后的应该是这样的
dlgFile.m_ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_HIDEREADONLY;
dlgFile.m_ofn.lpstrFilter = _T("Bin Files(*.bin)\0*.bin\0All Files(*.*)\0*.*\0\0");
const int nMaxFiles = 1000;
const int nMaxPathBuffer = (nMaxFiles * (MAX_PATH + 1)) + 1;
LPSTR pc = (LPSTR)malloc(nMaxPathBuffer * sizeof(WCHAR));
if( pc )
{
dlgFile.GetOFN().lpstrFile = pc;
dlgFile.GetOFN().lpstrFile[0] = NULL;
dlgFile.m_ofn.nMaxFile = nMaxPathBuffer;
if (dlgFile.DoModal() != IDOK)
{
return;
}
UpdateData(TRUE);
POSITION pos = dlgFile.GetStartPosition();//获取第一个文件位置
UpdateData(FALSE);
while (pos)
{
m_FilePathName[i] = dlgFile.GetNextPathName(pos);
if(i == 0)
{
m_TreeMuti1.DeleteAllItems();
/*TiXmlDocument *doc1 = new TiXmlDocument("D:\\sumA.bin");*/
TiXmlDocument *doc1 = new TiXmlDocument(m_FilePathName[0]);
/*CString str;str.Format(_T("%s"),m_FilePathName[0]);AfxMessageBox(str);*/
if (!doc1->LoadFile())
{
/*CString str;str.Format(_T("%s"),m_FilePathName[0]);AfxMessageBox(str);*/
return;
}
TiXmlElement *root1=doc1->RootElement();
TiXmlElement *pNode1=root1;
CrcXmlMuti1(pNode1,TVI_ROOT);
修改后的应该是这样的
dlgFile.m_ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_HIDEREADONLY;
dlgFile.m_ofn.lpstrFilter = _T("Bin Files(*.bin)\0*.bin\0All Files(*.*)\0*.*\0\0");
const int nMaxFiles = 1000;
const int nMaxPathBuffer = (nMaxFiles * (MAX_PATH + 1)) + 1;
LPSTR pc = (LPSTR)malloc(nMaxPathBuffer * sizeof(WCHAR));
if( pc )
{
dlgFile.GetOFN().lpstrFile = pc;
dlgFile.GetOFN().lpstrFile[0] = NULL;
dlgFile.m_ofn.nMaxFile = nMaxPathBuffer;
if (dlgFile.DoModal() != IDOK)
{
return;
}
UpdateData(TRUE);
POSITION pos = dlgFile.GetStartPosition();//获取第一个文件位置
UpdateData(FALSE);
while (pos)
{
m_FilePathName[i] = dlgFile.GetNextPathName(pos);
if(i == 0)
{
m_TreeMuti1.DeleteAllItems();
/*TiXmlDocument *doc1 = new TiXmlDocument("D:\\sumA.bin");*/
TiXmlDocument *doc1 = new TiXmlDocument(m_FilePathName[0]);
/*CString str;str.Format(_T("%s"),m_FilePathName[0]);AfxMessageBox(str);*/
if (!doc1->LoadFile())
{
/*CString str;str.Format(_T("%s"),m_FilePathName[0]);AfxMessageBox(str);*/
return;
}
TiXmlElement *root1=doc1->RootElement();
TiXmlElement *pNode1=root1;
CrcXmlMuti1(pNode1,TVI_ROOT);
展开全部
m_FilePathName[0]在使用之前用format格式化一次,有可能里面存放的数据格式错了。
m_FilePathName[0].Format("%s",m_FilePathName[0]);
TiXmlDocument *doc1 = new TiXmlDocument(m_FilePathName[0]);
m_FilePathName[0].Format("%s",m_FilePathName[0]);
TiXmlDocument *doc1 = new TiXmlDocument(m_FilePathName[0]);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这样试试
//本代码测试环境VS2008 c++ MFC
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,
NULL, this);
dlg.m_ofn.lpstrInitialDir = (BSTR)directoryName; //设置对话框默认呈现的路径
CString strFilePath;
if(dlg.DoModal() == IDOK)
{
CArray<CString, CString> aryFilename;
POSITION posFile=dlg.GetStartPosition();
while(posFile!=NULL)
{
aryFilename.Add(dlg.GetNextPathName(posFile));
}
for(int i=0;i<aryFilename.GetSize();i++)
{
if(strFilePath.GetLength()>0)
{
strFilePath.AppendChar(';');//多个文件‘;’分开
}
strFilePath+= aryFilename.GetAt(i);
}
}
仅供参考
//本代码测试环境VS2008 c++ MFC
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,
NULL, this);
dlg.m_ofn.lpstrInitialDir = (BSTR)directoryName; //设置对话框默认呈现的路径
CString strFilePath;
if(dlg.DoModal() == IDOK)
{
CArray<CString, CString> aryFilename;
POSITION posFile=dlg.GetStartPosition();
while(posFile!=NULL)
{
aryFilename.Add(dlg.GetNextPathName(posFile));
}
for(int i=0;i<aryFilename.GetSize();i++)
{
if(strFilePath.GetLength()>0)
{
strFilePath.AppendChar(';');//多个文件‘;’分开
}
strFilePath+= aryFilename.GetAt(i);
}
}
仅供参考
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询