在MFC中用CFile类来进行文件读写
CFile file; //创建文件类
file.Open("xx.txt",CFile::modeCreate|CFile::modeReadWrite); //创建文件以读写方式打开
file.Write("lalala",6); //写文件
file.Colse(); //关闭
要读的话
unsigned char buf[MAX];
file.Read(buf,MAX);
还可以获取文件信息
CFileStatus fs; //这个结构保存文件信息,写时会自动创建
file.GetStatus(fs); //读取文件信息
如果要提取文件创建时间
CString ctime=fs.m_ctime.Format("%Y-%m-%d %H:%M:%S"); //把时间格式化到ctime
串行化操作
CArchive ar(&file,CArchive::store); //定义串行化写类的变量,以储存方式
现在如果需要写,只需用C++的符号<<
如ar<<data;
当然如果要读
CArchive ar(&file,CArchive::load);
然后用ar>>data
补充:格式化显示strText.Format();//相当于C语言printf格式化,当然strText为CString类