Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get system command output in C program

Is there a better way to do it?

int numOfCPU;
system("grep -c ^processor /proc/cpuinfo >> /tmp/cpuinfo");
FILE *fp = fopen("/tmp/cpuinfo", "r");
fscanf(fp, "%d", &numOfCPU);
fclose(fp);
system("rm /tmp/cpuinfo");

I don't want to create an intermediary file and then remove it.

EDIT:

Its not about reading from the file. The command can be "ls" or "echo 'Hello world'"

like image 649
Vikas Goel Avatar asked Jun 14 '13 11:06

Vikas Goel


People also ask

How do I get the output of a command file?

Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

What does system () do in C?

The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.

How do I find the output of a program?

You can get the output after running a script using a pipe. We use pipes when we want the output of the child process. So here is the script, which you want to run. Put it in a command variable with the arguments your script takes (nothing if no arguments).

How can I see the output of C program in Linux?

How do I compile and see the output of C program in a Linux when using Fedora Linux? Under Linux you need to use the cc/gcc (GNU project C and C++ compiler) command to compile a program written in C or C++. When you compile a program it generates an executable file called a. out.


2 Answers

Ok, I was confused in my other answer. In any case, the philosophy in this answer is the same. You can use directly the popen function.

Then you have something like this:

int numOfCPU;
FILE *fp = popen("grep -c ^processor /proc/cpuinfo", "r");

fscanf(fp, "%d", &numOfCPU);
pclose(fp);

I hope it will be useful.

like image 62
castarco Avatar answered Oct 19 '22 21:10

castarco


You need to use redirection and pipes to do what you are trying to do.

The popen call can help you, but if you want something more flexible, such as also redirecting input, or more secure, such as not running a string in the shell, you should follow this example, taking from the manual page of pipe.

   #include <sys/wait.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <string.h>

   int
   main(int argc, char *argv[])
   {
       int pipefd[2];
       pid_t cpid;
       char buf;

       if (argc != 2) {
        fprintf(stderr, "Usage: %s <string>\n", argv[0]);
        exit(EXIT_FAILURE);
       }
       if (pipe(pipefd) == -1) {
           perror("pipe");
           exit(EXIT_FAILURE);
       }

       cpid = fork();
       if (cpid == -1) {
           perror("fork");
           exit(EXIT_FAILURE);
       }

       if (cpid == 0) {    /* Child reads from pipe */
           close(pipefd[1]);          /* Close unused write end */

           while (read(pipefd[0], &buf, 1) > 0)
               write(STDOUT_FILENO, &buf, 1);

           write(STDOUT_FILENO, "\n", 1);
           close(pipefd[0]);
           _exit(EXIT_SUCCESS);

       } else {            /* Parent writes argv[1] to pipe */
           close(pipefd[0]);          /* Close unused read end */
           write(pipefd[1], argv[1], strlen(argv[1]));
           close(pipefd[1]);          /* Reader will see EOF */
           wait(NULL);                /* Wait for child */
           exit(EXIT_SUCCESS);
       }
    }

You should modify the child process to use dup2 to redirect the standard output to the pipe and then exec the command you want it to run.

like image 30
LtWorf Avatar answered Oct 19 '22 22:10

LtWorf