开发者

Get the output of system() in C

I would like to put the number given by system("stat 开发者_开发技巧-f %g /dev/console") in a variable (working in Xcode using C). How is this best achieved?


I'd rather use the stat function :

struct stat file_details;
stat("/dev/console", &file_details);
printf("group id : %ld\n", (long) file_details.st_gid);


int variable;
variable = system("stat-f %g /dev/console");


You need to use popen rather than system if you want to capture the output of a command. E.g.

#include <stdio.h>

int main(void)
{
   int id = -1;
   FILE *fp = popen("stat -f %g /dev/console", "r");

   if (fp != NULL)
   {
       fscanf(fp, "%d", &id);
       pclose(fp);
   }

   printf("id = %d\n", id);

   return 0;
}


#include<stdlib.h>

...

int return_value = system("stat-f %g /dev/console");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜