Write program logs with CSTDIOFILE

xiaoxiao2021-03-06  67

Use the CSTDIOFILE Write Log assume that the log saves in the application directory, the file name is logfile.log calls the following function to get the current application directory, / * * // * Function name: getAppdir * / / * Description: Get Application Directory ******************************************************************************************************************************************************************************************************* *********************************** / VOID GETAPPDIR (CSTRING & Strappdir) {tchar szfullpath [Max_Path]; tchar szdir [_MAX_DIR]; tchar szdrive [_max_drive ]; // Get application's full path :: GetModuleFileName (NULL, szFullPath, MAX_PATH);. TRACE (_T ( "% s / n"), szFullPath);. // Break full path into seperate components _splitpath (szFullPath, szDrive, SZDIR, NULL, NULL); // Store Application's Drive and Path Strappdir.Format (_T ("% s% s"), szdrive, szdir);} then use the function written similar to the following function Writing log void clogtestdlg :: OnButton1 () { // Todo: Add Your Control Notification Handler Code Here Cstdiofile F; CString Strapp; GetAppdir (strapp); cstring strlogfilename; strlogfilename = strapp "logfile.log";

Bool Bresult = f.open (strlogfilename, cfile :: modewrite | cfile :: modecreate | cfile :: modenotruncate | cfile :: typetext); if (! BRESULT) {Trace ("Open Error / N"); Return;} CTIME T = CTIME :: getCurrentTime (); static i = 0; cstring strwrite; strwrite.format ("[% 04d-% 02D-% 02D% 02D:% 02D:% 02D] this is test string% d / n", T.GETYEAR (), T.GETMONTH (), T.GETDAY (), T.GETHOUR (), T. Getminute (), T.GetSecond (), i ; f.seektoend (); f.writestring (Strwrite );}: 1. When opening the file, add the CFILE :: MODECREATE | CFILE :: MODENOTRUNCATE, where CFile :: MODECREATE creates files when the file does not exist, cfile: : MODENOTRUNCATE The role of MODENOTRUNCATE does not cut the file as 0, and if you do not add MODENOTRUNCATE, each time you open the file, the file will be truncated to 02. Before writing the file, you must seetore, otherwise the previous content will be overwritten. 3. Close the file after writing

转载请注明原文地址:https://www.9cbs.com/read-87156.html

New Post(0)