4

I would like to know how to substitue the value of a variable in C.

execl ("/bin/cat","cat","/proc/30828/status", (char *)0 );

I would like to be able to change the "30828" to a variable, because this value isn't fixed. I am wondering if it would be possible to do something like in SHELL where you can do it. For example in shell you can do:

K=`ls -lis $i`
echo $K
Jongware
  • 22,200
  • 8
  • 54
  • 100
user3671361
  • 175
  • 4
  • 15

1 Answers1

6

Use snprintf() to substitute the PID into a string variable:

char statusfile[30];
sprintf(statusfile, sizeof statusfile, "/proc/%d/status", pid);
execl("/bin/cat","cat",statusfile, (char *)0 );
Barmar
  • 741,623
  • 53
  • 500
  • 612