直接使用pwd不行,linux系统中有个符号链接:/proc/self/exe 它代表当前程序,我们可以用readlink读取它的源路径就可以获取当前程序的绝对路径。
标签:cnt,路径,程序,current,获取,linux,path,SIZE,absolute From: https://www.cnblogs.com/2353o/p/17446012.html
char current_absolute_path[MAX_SIZE]; //获取当前程序绝对路径 int cnt = readlink("/proc/self/exe", current_absolute_path, MAX_SIZE); if (cnt < 0 || cnt >= MAX_SIZE) { printf("***Error***\n"); exit(-1); } //获取当前目录绝对路径,即去掉程序名 int i; for (i = cnt; i >=0; --i) { if (current_absolute_path[i] == '/') { current_absolute_path[i+1] = '\0'; break; } } printf("current absolute path:%s\n", current_absolute_path);