C語言寫檔案或日誌

2021-06-22 15:35:31 字數 946 閱讀 7364

/*

功能: 獲取當前系統時間

返回值: 0-成功,-1-失敗

out: 儲存返回的系統時間,格式由fmt決定

fmt: 0-返回:yyyy-mm-dd hh24:mi:ss, 1-返回:yyyy-mm-dd, 2-返回:hh24:mi:ss

*/int gettime(char *out, int fmt) // 獲取當前系統時間

file* openfile(const char *filename, const char *mode) // 開啟文字檔案 /*

功能: 將str寫入到檔案

返回值: 寫檔案成功返回0,否則返回-1

fp: 檔案指標

str: 待寫入的字串

blog: 1-是日誌檔案,0-不是日誌檔案

說明: 如果是日誌檔案,將會在str前加上當前時間(格式如:2011-04-12 12:10:20)

*/int writefile(file *fp, const char *str, int blog) // 寫字串到檔案,blog表明是否為日誌檔案

; int ret = -1;

if(blog) // 獲取當前系統時間

else

ret = fprintf(fp, "%s\n", str);

if(ret >= 0)

else

return -1;}

int closefile(file *fp)

需要的標頭檔案有:

time.h, string.h, stdio.h, assert.h

在寫檔案的時候,一定要控制好開啟檔案時的mode,防止誤操作而刪除了原來檔案的內容。

C語言寫檔案或日誌

功能 獲取當前系統時間 返回值 0 成功,1 失敗 out 儲存返回的系統時間,格式由fmt決定 fmt 0 返回 yyyy mm dd hh24 mi ss,1 返回 yyyy mm dd,2 返回 hh24 mi ss int gettime char out,int fmt 獲取當前系統時間 ...

C語言寫日誌功能

1 非常簡單的c語言記錄檔案功能。define crt secure no warnings include include include include include define max file len 1024 1024 獲取當前系統時間 char getnowtime in路徑設定,o...

C語言寫檔案

一 fopen 函式 fopen 函式宣告在stdio.h標頭檔案,第乙個引數開啟包含該檔名的字串的位址,第二個引數是用於指定檔案開啟模式的乙個字串。二 getc 和putc 函式 ch get fp 從指標fp指定的檔案中會的乙個字元。putc ch,fp 將字元寫到fp 指定的檔案中。三 fcl...