日历

2008 7.4 Fri
  12345
6789101112
13141516171819
20212223242526
2728293031  
«» 2008 - 7 «»

日志分类

文章搜索

日志文章

2008年04月09日 15:30:16

C语言得到一个进程的全路径

一个进程的命令行保存在文件/proc/pid/cmdline中,参数之间是字节0分隔。下面的小程序举例说明如何用C语言去读这个文件。
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) ...{
if(argc != 2) ...{
  printf("usage: %s pid ", argv[0]);
  exit(0);
}

std::string path(argv[1]);
path = "/proc/" + path + "/cmdline";
std::ifstream fin(path.c_str());
if(!fin) ...{
  std::cout << "Open /proc/" << argv[1] << "/cmdline failed! ";
  exit(-1);
}
std::string s;
[img]http://www.itwis.com/upimg/allimg/080408/1716487.gif[/img] while (getline(fin, s, '\0'))
{
  std::cout << s << std::endl;
}
}

类别: 无分类 |  评论(0) |  浏览(195) |  收藏
发表评论