sven
sven

Reputation: 1121

copy the output of an executed command by the system() call to a char array

Is it possible to copy the output of a command used on system() function on Linux? E.g., If I run

#include <stdlib.h>

int main(int argc,char *argv[]){
    char date[8]; //e.g., 20130421 yyyymmdd
    char time[4]; // e.g., 0204 hhmm
    system("ntpdate");
    return 0;
}

it outputs:

21 Apr 02:12:56 ntpdate[32680]: no servers can be used, exiting

is it possible to copy the output to a string? how can I copy the date and time info to a char array e.g. char *date ; char *time on C?

Upvotes: 2

Views: 294

Answers (1)

Matteo Italia
Matteo Italia

Reputation: 126787

system does not allow such a thing, you have to use popen("ntpdate", "r"), which returns a FILE * from which you can read the command output.

Upvotes: 5

Related Questions