CC++常⽤接⼝:添加写⼊⽇志到⽂件的功能
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
int write_log (FILE* pFile, const char *format, ...) {
va_list arg;
int done;
va_start (arg, format);
time_t time_log = time(NULL);
struct tm* tm_log = localtime(&time_log);
fprintf(pFile, "%04d-%02d-%02d %02d:%02d:%02d ", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_
fprintf作用
done = vfprintf (pFile, format, arg);
va_end (arg);
fflush(pFile);
return done;
}
int main() {
FILE* pFile = fopen("", "a"); //没有该⽂件则新建,有则追加写⼊
write_log(pFile, "%s\n", "1234567890");
write_log(pFile, "%s %d\n", "abcdefg", 10);
write_log(pFile, "%s %d %f\n", "AAAAAAAAAAAAAAAA", 10, 55.55);
fclose(pFile);
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。