void CMy07124Dlg::OnBnClickedButtonRead()
{
CFile file;
CString strFileName = _T("data.txt");
char buf[1024] = {0};
try
{
if(!file.Open(strFileName, CFile::modeRead))
{
MessageBox(_T("no file"), _T("title"), MB_OKCANCEL | MB_ICONINFORMATION);
return ;
}
file.Read(buf, 1024);
file.Close();
MessageBoxA(this->m_hWnd, buf, "title", MB_OKCANCEL);
}
catch (CMemoryException* e)
{
}
catch (CFileException* e)
{
CString str;
str.Format(_T("读取数据失败的原因是:%d"), e->m_cause);
MessageBox(str);
file.Abort();
e->Delete();
}
catch (CException* e)
{
}
}
void CMy07124Dlg::OnBnClickedButtonWrite()
{
CString strWrite = _T("write");
CFile file;
try
{
file.Open(_T("write.txt"), CFile::modeCreate | CFile::modeWrite);
file.SeekToBegin();
file.Write((unsigned char *)(strWrite.GetBuffer(0)),
sizeof(TCHAR) * strWrite.GetLength());
file.Flush();
file.Close();
MessageBox(_T("write succeed"));
}
catch(CFileException *e)
{
CString str;
str.Format(_T("写入失败的原因是:%d"), e->m_cause);
MessageBox(str);
file.Abort();
e->Delete();
}
}
CFile_Read_Write
最新推荐文章于 2024-01-08 21:23:11 发布