嗨,我用C写了一个linux,我想把输出重定向到文件和终端,我发现tee是最好的选择。我访问了tee的linux手册页,发现tee可以用作C程序中调用的函数调用。所以我写了
int size =tee(pipeends[1], 1,INT_MAX,SPLICE_F_NONBLOCK);但这根本行不通。上面写着
函数‘tee’的隐式声明(pipeends1,1,INT_MAX,SPLICE_F_NONBLOCK);
我在互联网上搜索了很多,它返回的一切都是如何在终端中使用tee命令,我知道这是通过使用tee。但是我想在程序中对它进行编码,而不是让用户显式地输入它。我添加了头文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/stat.h>
#include< fcntl.h>作为我的linux shell代码的一部分。我不知道tee是否使用其他的头文件,但我没有头绪。
发布于 2019-02-15 10:47:33
手册页给出了必要的步骤:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <fcntl.h>这将带来一项声明,即:
ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags);因此,您应该能够编写一个程序,根据这些信息建立一个tee。请注意,调用是特定于Linux的,这不是一个标准C(也不是POSIX,Linux经常遵循的Unix标准)函数。
发布于 2019-02-15 10:43:18
似乎您还没有在文件中包含正确的标题:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <fcntl.h>你的问题中没有提到_GNU_SOURCE。也许你需要这个?
发布于 2019-02-18 05:06:10
其中包括:
#define _GNU_SOURCE
#include <fcntl.h>
ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags);`第三行是删除警告的那一行。(对我来说是这样)
另外,如果要在tee之后使用splice(),请使用以下宏:
ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);
#ifndef SPLICE_F_MOVE
#define SPLICE_F_MOVE 0x01
#endif
#ifndef SPLICE_F_NONBLOCK
#define SPLICE_F_NONBLOCK 0x02
#endif
#ifndef SPLICE_F_MORE
#define SPLICE_F_MORE 0x04
#endif
#ifndef SPLICE_F_GIFT
#define SPLICE_F_GIFT 0x08
#endif希望能帮上忙。我知道截止日期现在已经过去了:
https://stackoverflow.com/questions/54707530
复制相似问题