阅读背景:

windows下写日志文件的代码

来源:互联网 
void CreateLogFile() { ofstream file(LOG_FILE_PATH,ios::binary); file.close(); } bool isLogFileExist() { bool bRet = false; fstream file; file.open(LOG_FILE_PATH, ios::binary |ios::in); if(!file) { bRet = false; } else { bRet = true; } file.close(); return bRet; } void SaveLogFile(CString csLog) { ofstream file; file.open(LOG_FILE_PATH, ios::binary | ios::app); SYSTEMTIME st; GetLocalTime(&st); CString csYear; csYear.Format(_T("%4d"),st.wYear); CString csMonth; csMonth.Format(_T("%02d"),st.wMonth); CString csDay; csDay.Format(_T("%02d"),st.wDay); CString csHour; csHour.Format(_T("%02d"),st.wHour); CString csMinute; csMinute.Format(_T("%02d"),st.wMinute); CString csSecond; csSecond.Format(_T("%02d"),st.wSecond); CString strDate = csYear + _T("-") + csMonth + _T("-") + csDay + _T(" "); CString strTime = csHour + _T(":") + csMinute + _T(":") + csSecond; CStringA straDate(strDate); file<<straDate; CStringA straTime(strTime); file<<straTime; file<<_T(" "); file<<csLog; file<<'\n'; file.close(); } void WriteLogFile(CString csLogLine) { bool bExist = isLogFileExist(); if(!bExist) { CreateLogFile(); } SaveLogFile(csLogLine); } void CreateLogFile() { ofstream file(LOG_FILE_P



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: