在做项目过程中,需要在不同时刻保存数据到TXT文件。因此需要创建不同名字的文本文件。例如可以通过时间创建。
CString str = "C++";
CFile file; //文件流对象
SYSTEMTIME system; //时间流对象
GetLocalTime(&system); //得到当前系统时间
CString txtFilePath;
txtFilePath.Format(_T("./../../bin/x64/%d_%d_%d_%d_%d_%d_.txt"), system.wYear,system.wMonth,system.wDay, system.wHour, system.wMinute,system.wSecond);
file.Open(txtFilePath, CFile::modeCreate | CFile::modeWrite);
//数据保存
file.Write(str.GetBuffer(), str.GetLength() * sizeof(TCHAR));//str为CString类型
file.Flush();
file.Close();