When running the following code:
FILE *input;
char *name=NULL,*stat=NULL;
int i=0,j=0;
input=fopen("/proc/1/stat","r");
fscanf(input,"%d",&i);
fscanf(input,"%s",name);
fscanf(input,"%s",stat);
fscanf(input,"%d",&j);
printf("pid : %d name: %s status: %s ppid: %d",i,name,stat,j);
I get the output:
pid : 1 name: (null) status: (null) ppid: 0
The content of /proc/1/stat is
1 (init) S 0
Can you please tell me what i did wrong here?
nameandstatmust NOT be NULL… you must initialize them – by defining them directly as an array likechar name [LENGTH]…