我的代码:
void CMy1View::OnErjinzhi()
{
// TODO: Add your command handler code here
//UpdateData(true);
CFileDialog dlg(TRUE,NULL,NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, "All Files (*.*)|*.*|Text files(*.txt)|*.txt|Word documents(*.doc)|*.doc||",AfxGetMainWnd());
CString str;
if(dlg.DoModal()==IDOK)
{
str= dlg.GetPathName();//获取文件名,你可以用这个参数来对文件进行操作,比如fopen()
MessageBox(str);//这里举了个例子,用MessageBox来弹出你所选中的文件名。
}
CStdioFile f1; //CFile file;
CString FileName=str;
f1.Open(FileName,CFile::modeRead | CFile::typeBinary);
f1.Close();
AfxGetApp()->OpenDocumentFile(str); // UpdateData(false);//在文本框显示
MessageBox("读出成功!");
}
为何还是以文本方式输出呢?
请各位大侠指教~
void CMy1View::OnErjinzhi()
{
// TODO: Add your command handler code here
//UpdateData(true);
CFileDialog dlg(TRUE,NULL,NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, "All Files (*.*)|*.*|Text files(*.txt)|*.txt|Word documents(*.doc)|*.doc||",AfxGetMainWnd());
CString str;
if(dlg.DoModal()==IDOK)
{
str= dlg.GetPathName();//获取文件名,你可以用这个参数来对文件进行操作,比如fopen()
MessageBox(str);//这里举了个例子,用MessageBox来弹出你所选中的文件名。
}
CStdioFile f1; //CFile file;
CString FileName=str;
f1.Open(FileName,CFile::modeRead | CFile::typeBinary);
f1.Close();
AfxGetApp()->OpenDocumentFile(str); // UpdateData(false);//在文本框显示
MessageBox("读出成功!");
}
为何还是以文本方式输出呢?
请各位大侠指教~
是以二进制方式打开文件吗?
二进制方式读取,读取到内存,CString 默认把它转换为 char*字符串,然后就显示字符串了。
用AfxGetApp()->OpenDocumentFile(str);是不是只能以文本方式打开。