vc中数据保存为文本文件解决方法
vc中数据保存为文本文件
我是要将采集得到的一个物体直径大小这样一个数据(maxnumber)与其相对应的时间保存在文本文件中,保存的文件名为开始记录的时间,例如:开始记录时为20:10:36,则文件名为20:10:36.txt,文件内容为:
20:10:36 20
20:10:37 10
20:10:38 0
20:10:39 28
20:10:40 5
20:10:41 10
请大家帮我一下吧,谢谢。
------解决方案--------------------
CTime::GetCurrentTime,再Format,得到你要的时间格式
按照初始时间CFile::Open,再Write时间、"\t"和数据即可
------解决方案--------------------
文件名中是不充许:号的。
------解决方案--------------------
写个简单点的
。
我是要将采集得到的一个物体直径大小这样一个数据(maxnumber)与其相对应的时间保存在文本文件中,保存的文件名为开始记录的时间,例如:开始记录时为20:10:36,则文件名为20:10:36.txt,文件内容为:
20:10:36 20
20:10:37 10
20:10:38 0
20:10:39 28
20:10:40 5
20:10:41 10
请大家帮我一下吧,谢谢。
------解决方案--------------------
CTime::GetCurrentTime,再Format,得到你要的时间格式
按照初始时间CFile::Open,再Write时间、"\t"和数据即可
------解决方案--------------------
文件名中是不充许:号的。
------解决方案--------------------
写个简单点的
。
- C/C++ code
void WriteLog( int maxnumber) { try { CTime tm = CTime::GetCurrentTime(); CString strContent = tm.Format(_T("%H:%M:%S ")); strContent.Format(_T("%s %d"),strContent,maxnumber); CString strFileName= tm.Format(_T("%H%M%S.txt")); CStdioFile file; if( file.Open(strFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite) != 0) { file.SeekToEnd(); file.WriteString(strContent); file.WriteString(_T("\n\n")); file.Close(); } } catch(...) { } }
------解决方案--------------------
FILE *pFile = NULL;
CString strFilePath = L"Record.txt";
if ( 0 != _tfopen_s(&pFile, strFilePath, _T("w")))
{
return FALSE;
}
for(int i = 0; i < this->m_AccountInfoList.GetCount(); i++)
{
AccountInfo accountInfo = m_AccountInfoList.GetAt(m_AccountInfoList.FindIndex(i));
CString str = accountInfo.strAccountNo + _T(",");
str = str + accountInfo.strName + _T(",");
str = str + accountInfo.strPassword + _T(",");
str = str + accountInfo.cyAmount + _T("\n");
LPWSTR lptstr = str.GetBuffer();
LPSTR lpstr = new CHAR[wcslen(lptstr) + 1];
int nLen = WideCharToMultiByte( CP_ACP, 0, lptstr, -1, lpstr,
wcslen(lptstr) + 2 , NULL, NULL );
//*(lpstr + (wcslen(lptstr) )) = '\0';
fwrite(lpstr, sizeof(char), strlen(lpstr), pFile);
delete lpstr;
}
char pch[] = "AccountInfoEnd\n";
fwrite(pch, sizeof(char), strlen(pch), pFile);
CString str;
m_RecordEdit.GetWindowText(str);
str += _T("\n");
LPWSTR lptstr = str.GetBuffer();
LPSTR lpstr = new CHAR[wcslen(lptstr)*2 + 1];
int nLen = WideCharToMultiByte( CP_ACP, 0, lptstr, -1, lpstr,
wcslen(lptstr) * 2 , NULL, NULL );
*(lpstr + (wcslen(lptstr)*2 )) = '\0';
fwrite(lpstr, sizeof(char), strlen(lpstr), pFile);
fclose(pFile);
str.ReleaseBuffer();
delete lpstr;
------解决方案--------------------
给一个日志程序,只要稍改一下就能实现你的功能了。
// 写字串到日志文件,文件不分行
void CWriteLog::WriteStrToLog(CString str)
{
if(!theApp.GetProfileIntW(_T(WORKER_PARAM),_T(USE_LOG),0))
{
// 不使用日志
return;
}
// 文件操作
// 首先打开目录,如果目录不存在,则创建它
// 打开相对目录的文件,如果文件不存在,则先建立它,文件已经存在时在文件的尾部写入记录
CStdioFile fl;
CString lpStr2=_T(LOG_DIRECTORY_NODOT); // 日志目录名
lpStr2=CStaticData::GetDir()+lpStr2;
CString logName,st;
//////////// 取时间做为日志文件名 //////////////////////
CTime m_Time=CTime::GetCurrentTime();
st=m_Time.Format(_T("%Y%m%d"));
//////////////////////////////////////////////
int len;
char bt[256];
/////////////// UNICODE编码 转换成 ANSI编码 //////////////////////
//// 为了TXT文件方式中能显示UNICODE编码的汉字,必须对字符进行转换,否则只能显示ASCII码
// ************** 本处将字串中的内容,长度为len,转换到bt中 ***************
///////////////////////////////////////////////////////////////////////////////