Like Wildwizard and Sergei Steshenko clearly told you, sowpra, the
system() function call works. When the command specified executes successfully,
system() will return zero.
The problem is that you somehow expect
system() to capture the output, and give it to you as an integer. It does not do that.
To capture the output from a command, use
popen(). It starts the command, and captures its output as a file-like stream (if given
"r" in the second parameter).
popen() returns the handle for that stream. You need to keep reading from the stream, otherwise the kernel will "pause" (proper term is "block") the command until your process consumes the stream.
To capture the output of a command as an integer, you could use for example
Code:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <errno.h>
/* Execute the specified command, and parse the output as an integer.
* result can be NULL, if you don't need the integer for anything.
* Function returns 0 if successful, error code (errno) otherwise.
*/
int run_and_parse_int(const char *const command, int *result)
{
FILE *cmd;
int c, value, saved_errno;
/* Save errno, so we can keep it unchanged if successful. */
saved_errno = errno;
/* Invalid command? */
if (!command | !*command)
return errno = EINVAL;
/* Start command. */
errno = ENOMEM;
cmd = popen(command, "r");
if (!cmd)
return errno;
/* Scan command output for a decimal number. */
if (fscanf(cmd, "%d", &value) != 1) {
/* Failed. Kill the command. */
pclose(cmd);
return errno = EIO;
}
/* Save the result. */
if (result)
*result = value;
/* Ignore the rest of the command output. */
do {
c = getc(cmd);
} while (c != EOF);
/* Check if the command executed successfully. */
errno = EIO;
if (pclose(cmd))
return errno;
/* Success. Retain original errno. */
errno = saved_errno;
return 0;
}