C++获取程序所在目录

#include 
#include 
#include 

using namespace std;

string selfPath();

int main() {
	string abspath = selfPath();
	cout << abspath << endl;
	return 0;
}

string selfPath() {
	char buff[1024];
	ssize_t len = readlink("/proc/self/exe", buff, sizeof(buff)-1);
	if(len != -1) {
		buff[len] = '\0';
		return dirname(buff);
	}
	return "";
}

标签: none

已有 2 条评论

  1. [code lang=text]好深闹的样子。。[/code]

    1. [code lang=text]是呀,太深闹了。。。[/code]

添加新评论