我有一个我正在做的套接字select(),等待其他进程写入.一旦写入,我读取数据,并将其写入另一个文件描述符.我的问题是,如果有一种方法可以将套接字桥接到文件描述符,那么当数据就绪时,它会自动写入另一个文件描述符?
这样,我可以抛出我正在使用的缓冲区,并省略系统中的一个线程.
这是关于splice()的另一个问题.我希望用它来复制文件,我试图使用两个拼接调用,通过像splice维基百科页面上的例子一样的管道连接.我写了一个简单的测试用例,它只试图从一个文件读取前32K字节并将它们写入另一个文件:
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
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;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,输入文件似乎正确读取,但第二次拼接调用失败了EINVAL.谁知道我在这里做错了什么?
谢谢!
我想将数据从一个流复制到另一个流.现在通常,我会这样做:
n = fread(buffer, 1, bufsize, fin);
fwrite(buffer, 1, n, fout);
Run Code Online (Sandbox Code Playgroud)
有没有直接将数据从写一个办法fin来fout,没有经过缓冲准备,即代替fin->buffer->fout,我想直接做fin->fout(无缓冲).
是否可以在ANSI C中这样做?如果没有,是否可以使用POSIX功能?或者特定于Linux的解决方案?