这是关于splice()的另一个问题.我希望用它来复制文件,我试图使用两个拼接调用,通过像splice维基百科页面上的例子一样的管道连接.我写了一个简单的测试用例,它只试图从一个文件读取前32K字节并将它们写入另一个文件:
#define _GNU_SOURCE
#include
#include
#include
#include
#include
int main(int argc, char **argv) {
int pipefd[2];
int result;
FILE *in_file;
FILE *out_file;
result = pipe(pipefd);
in_file = fopen(argv[1], "rb");
out_file = fopen(argv[2], "wb");
result = splice(fileno(in_file), 0, pipefd[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
printf("%d\n", result);
result = splice(pipefd[0], NULL, fileno(out_file), 0, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
printf("%d\n", result);
if (result == -1)
printf("%d - %s\n", errno, strerror(errno));
close(pipefd[0]);
close(pipefd[1]);
fclose(in_file);
fclose(out_file);
return 0;
}
当我运行它时,输入文件似乎正确读取,但第二次拼接调用失败了EINVAL.谁知道我在这里做错了什么?
谢谢!