CFile记录日志——写各种数据类型的日志(CFile读写包括编码UTF-8)

一. 打印DWORD类型日志

myfile.Open(L"C:\\tempLog\\wtTempLog.txt", CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite );
CString info;
info.Format(_T("%u_"), dwTimeCode);
int len = info.GetLength();
		
myfile.Write(info.GetBuffer(), len * sizeof(TCHAR));
myfile.Close();

 

例2:CFile换行+追写

CFile file;
	CString strSendJson = _T("helloWorld");
	CString line = _T("\r\n");
	file.Open(_T("D:\\wtLog\\SendJson.txt"),CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
	for(int i = 0;i<10;i++){
		
		//strSendJson += _T("\r\n");
		int nlen = strSendJson.GetLength();
		file.SeekToEnd();
		file.Write(strSendJson.GetBuffer(),nlen*sizeof(TCHAR));
		file.Write(line.GetBuffer(),line.GetLength()*sizeof(TCHAR));
		//file.Write("\r\n",2);
	}
    file.Close();

例3:CFile对中文的输入与输出

输入:要想让字处理软件识别unicode必须在文件头上加入unicode编码的前导字符:0xff, 0xfe。

CFile inFile(L"D:\\wtLog\\sea.xml",CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);;
	CString strLine=_T("张三、李四");
	WORD unicode = 0xFEFF;  //这句重要,注意这里是F E FF,中间有个E
	inFile.SeekToBegin();
	inFile.Write(&unicode,2);  //这句重要

	for(int i = 0;i<3;i++){
		inFile.SeekToEnd();
	   inFile.Write(strLine,wcslen(strLine)*sizeof(wchar_t));  //这句重要
	}
	inFile.Close();

输出:

CFile file(L"D:\\wtLog\\large.xml",CFile::modeRead); 
	char *pBuf; 
	ULONGLONG iLen=file.GetLength(); 
	pBuf=new char[iLen+1]; 
	file.Read(pBuf,iLen); 
	pBuf[iLen]=0; 
	//CString str1(pBuf); 
	CString str1=CA2W(pBuf,CP_UTF8); //Utf8格式文件用此方法 
	delete[] pBuf; 
	file.Close();

看了一下除了CP_UTF8头文件还有其他的选项。

二. WORD类型

用%d

二. 一个例子

以现在系统时间为文件名(精确到毫秒),然后用CFile记录日志内容。

注意:1. CFile::modeCreate只可以创建文件,所以文件夹是必须事先就存在的。

             2. 特殊字符来命名文件可能会导致文件open失败。

SYSTEMTIME st;
		CString t0;
		GetLocalTime(&st);
		WORD t_year = st.wYear;
		WORD t_month = st.wMonth;
		WORD t_day = st.wDay;
		WORD t_hour = st.wHour;
		WORD t_minute = st.wMinute;
		WORD t_second = st.wSecond;
		WORD t_milisecond = st.wMilliseconds;
	

		t0.Format(_T("%4d-%2d-%2d_%2d-%2d-%2d-%3d"),t_year,t_month,t_day,t_hour,t_minute,t_second,t_milisecond);


		*SYSTEMTIME st;
		CString filename;
		GetLocalTime(&st);
		filename.Format(_T("%4d-%2d-%2d_%2d:%2d:%2d"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);*/
		
		CString t1 = CString(_T("D:\\test\\"));
		CString t2 = CString(_T(".txt"));
		CString filename2;
		filename2.Format(_T("%s%s%s"),t1,t0,t2);
		//strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond) ;
		
		CString t_info = CString(_T("hello"));
        int len = t_info.GetLength();

		CFile file;
		bool ret = file.Open(filename2,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
		file.Write(t_info.GetBuffer(),len * sizeof(TCHAR));  
		file.Close();

三. 写成的文件是UTF-8编码(传递参数为1.unsinged char*    2.const char*等)

要求传递参数是unsigned char*写成utf-8文件

CFile inFile(L"D:\\wttttLog.txt",CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);;
char *g_pCharXML=NULL;
CString strContentAA = CString(_T("#EXTINF:10.000, Title: tt5_252__000__high_0.ts"));
CString strContentBB = CString(_T("\r\n"));
CString strContent;
strContent.Format(_T("%s%s"),strContentAA,strContentBB);
g_pCharXML=NULL;
int g_pcharSize =0;

int utf8size = WideCharToMultiByte(CP_UTF8, 0, strContent, -1, NULL, 0, NULL, NULL);  

if(g_pcharSize<utf8size)
{
	if(g_pCharXML)delete g_pCharXML;

	g_pCharXML = new char[utf8size];
	g_pcharSize = utf8size;
}
memset(g_pCharXML,0,utf8size);
WideCharToMultiByte(CP_UTF8, 0, strContent, -1, g_pCharXML, utf8size, NULL, NULL);  
		
//inFile.Write(g_pCharXML,strlen(g_pCharXML));
inFile.Write((unsigned char*)g_pCharXML,strlen(g_pCharXML));
inFile.Close();

其他的参数要求写UTF-8格式

BOOL WriteALine(CStdioFile& phfile, CString strLine)
{

	if(phfile.m_hFile)
	{
		DWORD dwFileLen = phfile.GetLength();

		if (0 == dwFileLen) 
		{
			const unsigned char LeadBytes[]  = {0xEF, 0xBB, 0xBF};
			// <<HLS-WWDC-2017-Preliminary-Spec>>4.1节:Playlist files MUST be encoded in UTF-8 [RFC3629]. They MUST NOT
			// contain any byte order mark (BOM); Clients SHOULD reject Playlists
			// which contain a BOM or do not parse as UTF-8.
			// phfile.Write(LeadBytes, sizeof(LeadBytes));
		}

		int nSrcLen = (int)wcslen(strLine);

		CStringA utf8String(strLine);
		int nBufLen = (nSrcLen+1) * 6;
		LPSTR buffer = utf8String.GetBufferSetLength(nBufLen);

		int nLen = AtlUnicodeToUTF8(strLine, nSrcLen, buffer, nBufLen);

		buffer[nLen] = 0;
		utf8String.ReleaseBuffer();

		phfile.SeekToEnd();
		phfile.Write((LPCSTR)utf8String, nLen);
		phfile.Write("\n", 1);
	}
	else
		return FALSE;

	return TRUE;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值