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

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