表頭文件:
#include <limits.h>
#include <stdlib.h>
函數原型:
char *realpath(const char *path, char *resolved_path)
函數說明:
realpath()用來將參數path所指的相對路徑轉換成絕對路徑后存於參數resolved_path所指的字符串數組或指針中
返回值:
成功則返回指向resolved_path的指針,失敗返回NULL,錯誤代碼存於errno
示例代碼:
#include <limits.h> /* PATH_MAX */ #include <stdio.h> #include <stdlib.h> /* realpath */ int main() { char actualPath[PATH_MAX + 1] = {0}; char* ptrRet = NULL; FILE* fp = NULL; while (1) { ptrRet = realpath("/home/tongyishu/debug.log", actualPath); if (ptrRet != NULL) { fprintf(stdout, "%s\n", actualPath); fp = fopen(actualPath, "a"); if (fp != NULL) { fprintf(stdout, "tongyishu\n"); fprintf(fp, "tongyishu\n"); sleep(3); } fclose(fp); } } return 0; }
控制日志開關:
在上述函數中,如果debug.log不存在,則不會打印日志
如果debug.log文件已經存在,則會每隔3秒打印一次
因此,可以通過控制 debug.log 文件的否存在來控制日志的打印