利用呼叫system("ls") 執行外部程式,將結果存在 buffer 中, 而不是直接輸出到console
反正我這邊以後一定會用到,這邊做個紀錄
@HOST:~/tmp$ cat redir.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define BUF_SIZE 1000
int main(void)
{
int fd[2];
char buffer[BUF_SIZE];
pipe(fd);
dup2(fd[1], 1);
memset(buffer, 0x00, BUF_SIZE);
system("ls");
fprintf(stderr,"**** output ****\n");
read(fd[0], buffer, BUF_SIZE);
fprintf(stderr, "%s", buffer);
return 0;
}
@HOST:~/tmp$ gcc -Wall redir.c -o redir
@HOST:~/tmp$ ./redir
**** output ****
dump.txt
redir
redir.c
@HOST:~/tmp$
No comments:
Post a Comment