My question is something I find very weird. In fact, I'm listing all the child processes of a given process.
For this, I use : How to find all child processes? and RC's answer.
But, I need an exit with unsigned long.
And the solution give some string based on a char[]
If I want to get unsigned long
, I need to modify the code to get only numeric value. The other case will not be correct.
So I've put the char
as unsigned long
. Then, the scanf
with %ld
tags.
But I'm getting crap ...
Why does that not work ?
EDIT : Code :
const char *name = "top";
char command[100];
unsigned long parentID; unsigned long processID;
strcpy(command, "ps -C "); strcat(command, name); strcat(command, " --format '%P %p'");
FILE *fp = popen(command, "r");
if (fp == NULL) fprintf(stderr, "Error in reading PID child processes information !\n");
while(fscanf(fp, "%lu %lu", parentID, processID) != EOF) {
printf("PID : %lu Parent : %lu \n", processID, parentID);
}
return processID;
With, %s
and char[256]
, this work !
EDIT : Toughts
I was thinking of two ways to do this:
- First, using
unsigned long
instead ofchar[]
. - Second, convert
char[]
tounsigned long
after reading the file. However, I'd need to read it line by line. Is it possible withpopen
?