How to redirect the output of system() to a file?
问题 In this C program #include <stdio.h> #include <fcntl.h> int main() { int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU); dup2(stdout, file); system("ls -l"); return 0; } I'm trying to redirect the output of system() to a file, for that i have used dup2 but it is not working. What's wrong with this code ? and, please tell me if there is any better way to do this ? (without using > at the terminal ) 回答1: stdout is a FILE * pointer of the standard output stream. dup2 expects file descriptor,