C語言realpath


表頭文件:

    #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 文件的否存在來控制日志的打印                                                                        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM