-1

I recently wrote this code to execute system command in C. I just want to measure time performed by this system command.

However, I don't want the output results to be displayed upon executing this program.

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

How do I discard the output from the system command?

jww
  • 97,681
  • 90
  • 411
  • 885
user9179677
  • 87
  • 1
  • 6
  • 1
    If you don't want the result to be printed, where do you want them to go? – JawguyChooser Jan 05 '18 at 23:42
  • 1
    What's the point of listing files with `ls` at all? The whole point of running ls is to put a listing of files and folders on the terminal. If you don't want to do that, don't run ls. This is an XY problem - *I'm doing the wrong thing to get something done, but now it's causing me a second problem. How do I fix the second problem?* You don't; fix the first and the second goes away. – Ken White Jan 05 '18 at 23:43
  • 1
    If you want to get the list of files in a directory to operate on them within your program, use the `DIR` type. See here https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c – JawguyChooser Jan 05 '18 at 23:43
  • i just want to measure time performed by this system command. – user9179677 Jan 05 '18 at 23:44
  • 2
    Redirect output to >/dev/null – Tony Tannous Jan 05 '18 at 23:44
  • Part of that time is producing output on the terminal, so you're not *measuring time performed* at all. – Ken White Jan 05 '18 at 23:45
  • 1
    @user9179677: Why not just run `time ls >/dev/null`, and skip the wrapping? What is the wrapping in C doing (aside from adding overhead by changing a single process launch for `ls` into three, one for your program, one for the shell to run `ls` in, and one for `ls`)? – ShadowRanger Jan 05 '18 at 23:51
  • Why do you want to run a system command to list files, but not see what files are listed? I suggest `//system("ls");` That takes 0 time. – Weather Vane Jan 05 '18 at 23:56

2 Answers2

3

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

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

and if you want errors to also be suppressed

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

However, due to the overhead of running a shell and the fragility of constructing shell commands, it's better to avoid system() when you can.

that other guy
  • 116,971
  • 11
  • 170
  • 194
RooterTooter
  • 384
  • 1
  • 6
  • _system call_ is a well-defined term which may be confusing in this context. I think that some backticks can help: `system()` calls – myaut Jan 05 '18 at 23:58
  • It is indeed best to avoid `system()`. Its use of the shell and special signal-handling behavior give it a _single_ legitimate use: executing a shell command requested by the user (e.g., via `!` in `vi`). – Davis Herring Jan 06 '18 at 00:40
  • 1
    @DavisHerring Even then it's arguably better to run the user's login shell instead of `/bin/sh` as used by `system()` – that other guy Jan 06 '18 at 00:48
  • But my command `system("chcp 936")` there is no way to redirect the output, how? – Zhang Dec 04 '20 at 09:01
1

If you want to measure time by the system command, you can redirect standout and stderr in the system call. Not sure this is the most elegant, but it works:

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

Note that you need the <stdlib.h> headers for system.

JawguyChooser
  • 1,816
  • 1
  • 18
  • 32
  • You suggest redirecting stdout and stderr but the example you provide only redirects stdout. – Turn Jan 05 '18 at 23:53
  • @Turn you're right, I edited the answer. Although, fwiw, ls shouldn't really print anything on stderr ;) And we can only hope that whatever the OP is trying to achieve isn't the kind of thing which has to scale or account for weird edge cases because God knows this approach is sorta crazy in the first place. – JawguyChooser Jan 05 '18 at 23:56