我是靠谱客的博主 虚心睫毛,这篇文章主要介绍CFile处理文件的读写,现在分享给大家,希望可以做个参考。

1.创建

       //以日期为例设置文件名	
	CString filename;		
	filename.Format(__T("sim\%d%02d%02d_%02d%02d%02d.txt"), time.wYear, time.wMonth, time.wDay, m_sim.hour, m_sim.minute, m_sim.second);

	//将内容写入文件
	CFile file;
	
	if(file.Open(filename, CFile::modeCreate | CFile::modeWrite))
	{
		//文件打开成功
		b_OpenFile = true;
	}
	else
	{
		MessageBox(_T("存储文件打开失败!"));
		return ;
	}

2.写入字符串

//如果数据类型是CString可以直接用,如果是string类型则先转为CString
CString cstr;
file.Write(cstr.GetBuffer(), cstr.GetLength()*sizeof(TCHAR));
file.Write(_T("rn"), 2);        //每写入一条数据换行

3.写入数组

int *p = new int[10];

for(int i=0; i<10; i++)
{
file.Write(&p[i], sizeof(int));
file.Write(_T("rn"), 2);        //每写入一条数据换行
}

更多信息参考:https://blog.csdn.net/dodream/article/details/1687948

最后

以上就是虚心睫毛最近收集整理的关于CFile处理文件的读写的全部内容,更多相关CFile处理文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部