Windows写日志的两种方式

MFC的方式:

	CFile file;
	BOOL bData;
	if (data == nullptr || data_len <= 0)
		bData = FALSE;
	else
		bData = TRUE;
	CString str1, str2, str, str4;
	CString str3;

	CTime t = CTime::GetCurrentTime();
	str.Format(_T("%d_%d_%d.txt"), t.GetHour(), t.GetMinute(), t.GetSecond());
	str1.Format(_T("d:\\test\\%s"), str);
	file.Open(str1, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate);
	file.SeekToEnd();

	if (!bData)
	{
		data_len = 0;
		str3 = _T("");
	}
	else
	{

		str3 = (Public::mbcs_to_unicode((char *)data)).c_str();
	}


	str1.Format(_T("%d"), conn_id);
	str2.Format(_T("%d"), data_len);

	str4 = str1 + _T("  ") + str3 + _T("  ") + str2;
	file.Write(str4.GetBuffer(), str4.GetLength() * 2);


	str4.ReleaseBuffer();
	file.Close();

用C++ 流的方式

	SYSTEMTIME now;
	GetLocalTime(&now);
	char timeStr[64];
	sprintf_s(timeStr, sizeof(timeStr), "_%d-%02d-%02d_%02d-%02d-%02d_%03d.txt",
		now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond, now.wMilliseconds);
	
	std::string filePath = "d:\\test\\";

	SYSTEMTIME _now;
	::GetLocalTime(&_now);
	char _date[128] = { 0 };
	sprintf_s(_date, 128, "%04d-%02d-%02d", _now.wYear, _now.wMonth, _now.wDay);

	filePath += _date;

	Public::CreateRelativeDirectorys(filePath.c_str());
	filePath += timeStr;
	ofstream myfile(filePath, ios::out);

	unsigned char p[100000] = { 0 };
	std::string strData;
	strData = "";
	if (data == nullptr || data_len <= 0)
	{
		data_len = 0;
	}
	else
	{
		
		for (int i = 0; i < data_len + 1; i++)
			p[i] = data[i];
		std::string strFile((char*)p);
		strData = strFile;
	}
	
	myfile << conn_id << " \n" << strData << "\n " << data_len;
	myfile.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值