寻找指向的argv [0]这样我就可以改变它 [英] Finding pointer to argv[0] so I can change it
问题描述
在Solaris上我有一个指向的argv [0]
与 getexecname
,然后我可以的memcpy
在该位置。 (每<一个href=\"http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe/1024937#1024937\">Finding没有的/ proc /自/ EXE 当期可执行的路径)
On Solaris I got a pointer to argv[0]
with getexecname
and then I can memcpy
at that location. (per Finding current executable's path without /proc/self/exe)
我想知道如何得到一个指向的argv [0]
在Linux中我做了的readlink
在的/ proc /自/ EXE
,但它不给我一个指针。
I was wondering how to get a pointer to argv[0]
in Linux I did readlink
on /proc/self/exe
but it doesn't give me a pointer to it.
感谢
推荐答案
有关的readlink
,把你自己的缓冲区。您分配一个缓冲区,在一个指针传递给它,而的readlink
将有保存结果:
For readlink
, Bring Your Own Buffer. You allocate a buffer, pass in a pointer to it, and readlink
will store the results there:
#include <unistd.h>
#include <linux/limits.h>
int main() {
char buffer[PATH_MAX];
int size = readlink("/proc/self/exe", buffer, sizeof(buffer));
buffer[size] = '\0';
// buffer is now the char* holding the filename
printf("The executable is %s\n", buffer);
}
这篇关于寻找指向的argv [0]这样我就可以改变它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!