一. 打印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;
}