C / C ++ - 可执行路径 [英] C/C++ - executable path

查看:100
本文介绍了C / C ++ - 可执行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前可执行文件的文件路径,但没有可执行文件名称。

I want to get the current executable's file path without the executable name at the end.

我正在使用:

char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0)
    printf("executable path is %s\n", path);
else
    printf("buffer too small; need size %u\n", size);

它可以工作,但这会在末尾添加可执行文件名称。

It works, but this adds the executable name at the end.

推荐答案

dirname(path);
应该返回路径,也就是在Unix系统上,对于windows,你可以做一些strcpy / strcat魔术。

dirname(path); should return path without executable after you acquire path with, that is on Unix systems, for windows you can do some strcpy/strcat magic.

对于dirname,你需要包含 #include& h> ...

For dirname you need to include #include <libgen.h>...

这篇关于C / C ++ - 可执行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆