CFile 是MFC的类;
写txt 流程:CFile.open().。 CFile.write() CFile.chose().
virtual BOOL Open(
LPCTSTR lpszFileName,
UINT nOpenFlags,
CFileException* pError = NULL
);
virtual BOOL Open(
LPCTSTR lpszFileName,
UINT nOpenFlags,
CAtlTransactionManager* pTM,
CFileException* pError = NULL
);
首先找到当前路径;GetModuleFileName() 会直接定位到.exe路径; 在把.exe去掉,这样文件路径就可以确定了;
DWORD WINAPI GetModuleFileName(
__in_opt HMODULE hModule,
__out LPTSTR lpFilename,
__in DWORD nSize
);
CString log(_T(""));
CString strPath; //文件路径
TCHAR cPath[MAX_PATH + 1];
TCHAR cDiver[_MAX_DRIVE + 1];
TCHAR cDir[_MAX_DIR + 1];
GetModuleFileName(NULL, cPath, MAX_PATH);
_tsplitpath(cPath, cDiver, cDir, NULL, NULL);
strPath.Format(_T("%s"), cDiver, cDir,_T(".\\log.txt"));
下面就是写日志了; 这里小端 unicode 要加0xFEFF;
WORD unicode = 0xFEFF;
LogFile.Write(&unicode,2);
然后就写你自己要写的;
CString strlog(_T("............")); LogFile.Write(strLog,wcslen(strLog)*sizeof(wchar_t)); //注意长度错了 会乱码;wcslen和 wchar_t都是处理宽字符 最后 LogFile.chose(); //关闭文件