Linux下,c++獲取當前程序路徑

Linux下,c++獲取當前程序路徑

#include <stdio.h>
#include <unistd.h>

char *buffer;
buffer = getcwd(NULL, 0);
cout << "文件路徑" << buffer << endl;
//將需要調用的模塊使用 strcat 作拼接;
const char *model_path = strcat(buffer,"/models");

或者:

#include <stdio.h>
#include <unistd.h>
int main()
{
    char *buffer;
    if((buffer = getcwd(NULL, 0)) == NULL)
    {
        perror("getcwd error");
    }
    else
    {
        printf("%s\n", buffer);
    }
}
此程序是獲取當前程序的絕對路徑的方法.
有的文章包含的頭文件是<direct.h>,在linux下,貌似不是這樣的.
至於在那用到direct.h,這就不知道了.

參考自:
https://blog.csdn.net/liangxiaozhang/article/details/7704742?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章