※ 引述《lolo33 (none)》之銘言:
: 請問一下 我想要做到說 利用呼叫system("ls") 執行外部程式
: 但是我想要將結果存在 buffer 中, 而不是直接輸出到console
: 請問有辦法可以做到嗎?
: 感謝
參考一下吧
from http://zbarbar.blogspot.com/2009/08/blog-post_6471.html
@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$
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 116.59.127.243