2

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.