#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
int main(int argc, char ** argv) {
int fd[2];
int len,slen,len_out,slen_out;
pipe(fd);
int f=open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
len=tee(STDIN_FILENO,fd[1],INT_MAX,SPLICE_F_MOVE);
while (len > 0) {
slen = splice(fd[0], NULL, f, NULL, len, SPLICE_F_MOVE);
len -= slen;
if(slen==0)
break;
}
len_out = tee(STDIN_FILENO,fd[1],INT_MAX,SPLICE_F_MOVE);
while (len_out > 0) {
slen_out = splice(fd[0], NULL, STDOUT_FILENO, NULL, len_out, SPLICE_F_MOVE);
len_out -= slen_out;
if(slen_out==0)
break;
}
close(fd[1]);
close(fd[0]);
close(f);
return 0;
}
当我用管道ex:cat test.txt| ./a.out result.txt
运行程序时
但用另一种方法,例如:./a.out result.txt <test.txt
此外,当我拼接到STDOUT_FILENO
时,它从未打印在标准输出上,我不明白为什么.
谢谢你的帮助