Linux C 切换工作目录

发布于 2020-07-02  99 次阅读


size_t getProjectPath( char* processdir,char* processname, size_t len)
{
        char* path_end;
        if(readlink("/proc/self/exe", processdir,len) <=0)
                return -1;
        path_end = strrchr(processdir,  '/');
        if(path_end == NULL)
                return -1;
        ++path_end;
        strcpy(processname, path_end);
        *path_end = '\0';
        return (size_t)(path_end - processdir);
}

int main(int argc, char *argv[])
{
	//获取程序的绝对路径
	char proPath[PATH_MAX] = {0};
	char proName[64] = {0};
	getProjectPath(proPath, proName, sizeof(proPath));

	//改变工作目录
	chdir(proPath);
}