16,472
社区成员
发帖
与我相关
我的任务
分享
void CDemoDlg::OnReadFile()
{
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();
//创建文件对话框
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||"));
if (dlg.DoModal() == IDOK)
{
//获得文件路径
CString strPathName = dlg.GetPathName();
CStdioFile file;
//打开文件
if (!file.Open(strPathName, CFile::modeRead))
{
::AfxMessageBox(_T("文件打开失败。"));
return;
}
//读文件
CString strText = _T("");
while (file.ReadString(strText))
{
pListBox->AddString(strText);
}
//关闭文件
file.Close();
}
}
CString ii = _T("C:\\Users\\Administrator\\Desktop\\tesr.txt");
if (!myfile.Open(_T(ii), CFile::modeRead))
{
::AfxMessageBox(_T("文件打开失败。"));
return FALSE;
}