學習筆記 C獲取當前工作路徑

2021-08-19 11:46:18 字數 570 閱讀 9194

有一些引數需要用檔案讀寫操作,需要製作dll,呼叫時需要路徑的適應性,獲取dll所在路徑,獲得當前exe可執行檔案所在的路徑。

用 getcurrentdirectory(或者_getcwd),只能獲得當前工作路徑。如果**中對其他資料夾中的檔案操作時,則路徑不符合了。

一、獲得當前工作路徑

char   szfilepath_coef[max_path];

getcwd(szfilepath_coef, max_path);

strcat(szfilepath_coef, "\\he\\model_sv_coef-c2.txt");

二、獲得exe可執行檔案的路徑

char szfilepath_coef[max_path + 1] = ;

getmodulefilenamea(null, szfilepath_coef, max_path);

(strrchr(szfilepath_coef, '\\'))[0] = 0; // 刪除檔名,只獲得路徑字串  

strcat(szfilepath_coef, "\\he\\model_sv_coef-c2.txt");

獲取當前檔案路徑 當前工作目錄路徑

import os current dir os.path.abspath os.path.dirname file 當前檔案下的絕對路徑 print current dir 輸出 e weidian lc requestinfo current dir1 os.path.dirname file ...

獲取當前工作路徑(絕對路徑)

獲取當前工作目錄是使用函式 getcwd。cwd指的是 current working directory 這樣就好記憶了。函式說明 函式原型 char getcwd char buffer,int len 引數 buffer是指將當前工作目錄的絕對路徑copy到buffer所指的記憶體空間,len...

C 開啟檔案之獲取當前工作路徑Debug

在c 編寫程式時,如果需要獲取當前程式根目錄,通常使用system.environment.currentdirectory語句來獲取。並且在程式執行過程中,該語句獲取的路徑不變,始終是程式啟動目錄,一般為程式的安裝根目錄。但是在實際開發過程中發現,程式在windows7系統上可以正常執行,但在wi...