linux C語言獲取當前可執行程式的路徑及檔名 vs 獲取當前工作路徑
阿新 • • 發佈:2018-12-31
獲取當前工作路徑:
標頭檔案:#include <unistd.h>
定義函式:char * getcwd(char * buf, size_t size);
函式說明:getcwd()會將當前的工作目錄絕對路徑複製到引數buf 所指的記憶體空間,引數size 為buf 的空間大小。
獲取當前執行程式的路徑及檔名:
#include <limits.h> #include <stdio.h> #include <string.h> #include <unistd.h> size_t GetCurrentExcutableFilePathName( char* processdir,char* processname, size_t len) { char* path_end; if(readlink("/proc/self/exe", processdir,len) <=0) return -1; path_end = strrchr(processdir, '/'); if(path_end == NULL) return -1; ++path_end; strcpy(processname, path_end); *path_end = '\0'; return (size_t)(path_end - processdir); }