How to undo the output from a system command?

I recently wrote this code to execute a system command in C. I just want to measure the time that this system command takes.

However, I do not want output results to be displayed after running this program.

#include <stdio.h>
int main(int argc, char* argv[])
{
    system("ls");
    return 0;
}

How to undo the output from a system command?

-2
source share
2 answers

When you call system()from C, the shell is called to interpret your command. This means that you can use shell redirects:

system("ls > /dev/null");

and if you want errors to be suppressed as well

system("ls > /dev/null 2>&1");

However, due to the overhead of running the shell and the fragility of building shell commands, it is best to avoid system()when you can.

+2
source

, standout stderr . , , :

#include <stdlib.h>
#include <stdio.h>
int main() {
    system("ls >/dev/null 2>&1");
}

, <stdlib.h> system.

0

Source: https://habr.com/ru/post/1692866/


All Articles