轉自:http://blog.csdn.net/chaoyuan899/article/details/8780525
MFC中用Archive類寫入和讀取文件:
寫入數據:
//注:Graphic是工程的名字
void CGraphicView::OnFileWrite()
{
// TODO: Add your command handler code here
CFile file("1.txt",CFile::modeCreate|CFile::modeWrite);
CArchive ar(&file,CArchive::store);
int i=4;
char ch='a';
float f=1.3f;
CString str("hello word!");
ar<<i<<ch<<f<<str;
}
讀取數據:
void CGraphicView::OnFileRead()
{
// TODO: Add your command handler code here
CFile file("1.txt",CFile::modeRead);
CArchive ar(&file,CArchive::load);
int i;
char ch;
float f;
CString str;
CString strResult;
ar>>i>>ch>>f>>str; //注意,前面的按照怎樣的順序寫入數據的,在這裏就得按照怎樣的順序讀取數據
strResult.Format("%d,%c,%f,%s",i,ch,f,str);
MessageBox(strResult);
}
拓展閱讀:http://wenku.baidu.com/view/5175754ac850ad02de8041d1.html
方法二:
MFC中寫入/讀取文件,
寫入:
讀取: