MFC中用CArchive類寫入和讀取文件

轉自: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中寫入/讀取文件,

寫入:


讀取:

發佈了10 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章