c 读取文件 输出
来源:百度知道 编辑:UC知道 时间:2024/05/12 01:23:08
各位大哥 我是想用c++ 如何把一个类成员写入文件中 之后又能读取?
用串行化吧,给你段代码研究一下
//定义文件对象和文件异常对象
CFileException fe;
CFile file;
if(!file.Open(m_strFile/*文件名*/,CFile::modeWrite|CFile::modeCreate,&fe))
{
export_end = true;
fe.ReportError();
return false;
}
//构建CArchive 对象
CArchive ar(&file,CArchive::store);//如果读则CArchive ar(&file,CArchive::load);
try
{
CString strValue;
int nVal;
char cVal;
float fVal;
//写数据
ar << strValue << nVal << cVal << fVal;
//读数据
//ar >> strValue >> nVal >> cVal >> fVal;//上面必须是CArchive ar(&file,CArchive::load);
///如果读写的是对象,在对象里要实现串行化
}
catch ( CArchiveException *carchiveExeption )
{
carchiveExeption->ReportError();
goto ERROR1;
}
catch ( C