【发布时间】:2021-08-24 17:54:21
【问题描述】:
我正在尝试通过使用 cmdline 中的数据来提取调用应用程序的参数。
如果我像这样启动应用程序实例:
我的应用程序 1 2
然后 cat myapp 的命令行,我会看到类似 myapp12 的内容。
我需要提取这些值,我使用这段代码来完成它
pid_t proc_id = getpid();
sprintf(buf,"/proc/%i/cmdline",proc_id);
FILE * pFile;
pFile = fopen (buf,"r");
if (pFile!=NULL)
{
fread(buf,100,100,pFile);
cout << "PID " << proc_id << endl;
string str = buf;
cout << buf << endl;
size_t found=str.find_last_of("/\\");
cout << " file: " << str.substr(found+1) << endl;
fclose (pFile);
}
但我得到的只是应用名称,没有参数......
从答案复制的更新:
好吧,我现在的问题似乎是如何读取 cmdline 文件而不让它在第一个 NULL 字符处停止...
fopen(cmdline, "rb")
什么都不做,所以...
【问题讨论】:
-
今天打了这个之后,我想说明我必须使用:
fread(buf,1,100,pFile);(size=1, count=buffer length)。