C 读取stdin(并存储值)管道到子管道,执行剪切,然后返回值

C 读取stdin(并存储值)管道到子管道,执行剪切,然后返回值,c,pipe,fork,dup,C,Pipe,Fork,Dup,我正在做一个程序,它将接收3个参数,./a.out a b c,其中a和c是列号,b和操作数来自以: 当其为true时,将复制stdin,否则不会产生任何结果 示例: $ ./a.out 1 > 2 $ 5:2:1:6 5:2:1:6 $ ./a.out 2 = 4 $ 1:2:3:4 $ 我试过在我的第一个版本中,当剪切要求时,插入管道并读取标准输入,但我的问题是我丢失了输入 现在,我试图读取孩子体内的标准输入,存储并通过管道传递它,但是对于我的测试,我猜execlp没有得到标准输入

我正在做一个程序,它将接收3个参数,./a.out a b c,其中a和c是列号,b和操作数来自以:

当其为true时,将复制stdin,否则不会产生任何结果

示例:

$ ./a.out 1 > 2
$ 5:2:1:6
5:2:1:6

$ ./a.out 2 = 4
$ 1:2:3:4
$
我试过在我的第一个版本中,当剪切要求时,插入管道并读取标准输入,但我的问题是我丢失了输入

现在,我试图读取孩子体内的标准输入,存储并通过管道传递它,但是对于我的测试,我猜execlp没有得到标准输入

我不能用awk,它是用来做学术工作的

我现在的代码是:

int main(int argc, char const *argv[]){

    int n,f;
    char coluna1[16];
    char coluna2[16];
    char strin[PIPE_BUF];
    //put together args cut
    char buffer[PIPE_BUF];
    sprintf(buffer, "-f%s,%s",argv[1],argv[3]);

    //pipes
    int fd[2];
    int fd2[2];
    pipe(fd);
    pipe(fd2);

    if(!fork()) {
        close(fd[0]); //close read
        dup2(fd[1],1); //std output duplicated to pipe write
        close(fd2[0]); //close read
        //readline stdin
        n = read(0,strin,PIPE_BUF);
        write(fd2[1],strin,n);
        //cut -d: -f2,4 -
        execlp("cut","cut","-d:",buffer,"-",NULL);
    }
    //pai recebe do pipe
    close(fd[1]); //close write
    close(fd2[1]); //close write
    n = read(fd2[0],strin,PIPE_BUF); //read stdin from pipe
    f = read(fd[0],buffer,PIPE_BUF); //stdout from cut
    sscanf(buffer,"%[^:]:%s",coluna1,coluna2);

    //write the result from the cut to "bug check"
    write(1,buffer,f);

    //printfs just to check if everything worked
    if(strcmp(argv[2],"=")) if(atoi(coluna1) == atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO =\n"); }
    if(strcmp(argv[2],">=")) if(atoi(coluna1) >= atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO >=\n"); }
    if(strcmp(argv[2],"<=")) if(atoi(coluna1) <= atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO <=\n"); }
    if(strcmp(argv[2],">")) if(atoi(coluna1) > atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO >\n"); }
    if(strcmp(argv[2],"<")) if(atoi(coluna1) < atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO <\n"); }
    if(strcmp(argv[2],"!=")) if(atoi(coluna1) != atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO !=\n"); }

}
int main(int argc,char const*argv[]){
int n,f;
char coluna1[16];
char-coluna2[16];
字符串[PIPE_BUF];
//把args切块放在一起
字符缓冲区[PIPE_BUF];
sprintf(缓冲区,“-f%s,%s”,argv[1],argv[3]);
//管道
int-fd[2];
int-fd2[2];
管道(fd);
管道(fd2);
如果(!fork()){
关闭(fd[0]);//关闭读取
dup2(fd[1],1);//复制到管道写入的std输出
关闭(fd2[0]);//关闭读取
//读线标准
n=读取值(0,strin,PIPE_BUF);
写入(fd2[1],strin,n);
//切口-d:-f2,4-
execlp(“cut”,“cut”,“d:”,buffer,“-”,NULL);
}
//排管
关闭(fd[1]);//关闭写入
关闭(fd2[1]);//关闭写入
n=read(fd2[0],strin,PIPE_BUF);//从管道读取标准数据
f=读取(fd[0],缓冲区,管道_BUF);//从切割处读取
sscanf(缓冲区,“%[^:::%s”,coluna1,coluna2);
//将剪切结果写入“错误检查”
写入(1,缓冲器,f);
//printfs只是为了检查一切是否正常
if(strcmp(argv[2],“=”)if(atoi(coluna1)==atoi(coluna2)){write(1,strin,n);printf(“ACERTEI NO=\n”);}
if(strcmp(argv[2],“>=”)if(atoi(coluna1)>=atoi(coluna2)){write(1,strin,n);printf(“ACERTEI NO>=\n”);}
if(strcmp(argv[2],“\n”);}
如果(strcmp(argv[2]),“将其修复为:

if(!fork()) {
    close(fd[0]); //close read
    dup2(fd[1],1); //std output duplicated to pipe write
    close(fd2[1]); //close write
    dup2(fd2[0],0); //std input from father duplicated to pipe read
    //cut -d: -f2,4 -
    execlp("cut","cut","-d:",buffer,"-",NULL);
}
//father
close(fd[1]); //close write
close(fd2[0]); //close read
n = read(0,strin,PIPE_BUF);
write(fd2[1],strin,n);
close(fd2[1]);
//n = read(fd2[0],strin,PIPE_BUF); //read stdin from pipe
f = read(fd[0],buffer,PIPE_BUF); //stdout from cut

我看到的一个问题是,在许多shell中,“>”和“=”字符具有特殊的含义。在FD中使用文字也有点难看。我更喜欢宏来澄清这一点。在shell上尝试时,我使用“>“为了避免重新定向问题。宏的意思是像STDIN之类的?我说的宏是STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO。是的,引用或转义符号应该可以解决这类问题。”。