繁体   English   中英

双 pipe 将 output 保存到 C 中的文件

[英]Double pipe to save output to file in C

我创建了类似“linux pipe”的东西来将 output 从一个程序重定向到另一个程序,它的工作就像一个魅力。 但现在我想将最终的 output 保存到文件中。 在互联网上,我看到我需要创建双 pipe,但我创建的所有内容都不起作用。

这是我的“管道”实际代码:

 int pipes[2];
 int pid;
 pipe(pipes);
 if(pid=fork()){
         int fd = open("t.txt", O_CREAT | O_TRUNC | O_RDWR, 0644);
    if (fd < 0) {
        perror("open()");
        exit(EXIT_FAILURE);
    }
   close(pipes[1]);
   dup2(pipes[0],STDIN_FILENO);
    execvp(sec_command[0],sec_command);

 }
     else{

   close(pipes[0]);
   dup2(pipes[1],STDOUT_FILENO);
   execvp(first_command[0],first_command);
 }

谢谢

不,你不需要。 您只需要在子进程(它将从管道捕获其输入)中执行第二个dup2到您想要 output 到 go 的文件的文件描述符。 您还没有发布Minimal, Reproducible Example ,并且您的代码是错误的( exec在父进程和子进程中执行)所以我试图制定一个完整的解决方案。 这里是:

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

pid_t
subcommand_to_file(
        char **subcommand_args,
        char *file)
{
    int pipes[2];
    pipe(pipes);
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return -1;
    } else if (pid == 0) { /* child */
        int fd = open(file,
                O_CREAT | O_TRUNC | O_RDWR,
                0666); /* permissions controled by umask */
        if (fd < 0) {
            perror("open");
            exit(EXIT_FAILURE);
        }
        close(pipes[1]); /* close output descriptor of pipe */
        dup2(pipes[0], STDIN_FILENO); /* redirect input */
        close(pipes[0]); /* not needed after dup2 */
        dup2(fd, STDOUT_FILENO); /* rediect output */
        close(fd); /* not needed after dup2 */
        execvp(subcommand_args[0], subcommand_args);
        perror("execvp");
        exit(EXIT_FAILURE);
    }
    /* parent */
    close(pipes[0]); /* close input descriptor of pipe */
    dup2(pipes[1], STDOUT_FILENO);
    close(pipes[1]); /* not needed, after dup */
    return pid; /* so if you want to wait for the child */
}

int
main(   int argc,
        char **argv)
{
    int opt;
    char file[256];

    /* default value for filename */
    snprintf(file, sizeof file, "%s.out", argv[0]);

    while ((opt = getopt(argc, argv, "f:")) != EOF) {
        switch (opt) {
        case 'f':
            strncpy(file, optarg, sizeof file);
            break;
        }
    }

    argc -= optind;
    argv += optind;

    pid_t child = subcommand_to_file(argv, file);
    if (child < 0) {
        fprintf(stderr,
            "subcommand_to_file: %s\n",
            strerror(errno));
        exit(EXIT_FAILURE);
    }
    /* copy input to output to pass stdin to  subprocess */
    int c;
    while((c = getchar()) != EOF) {
        putchar(c);
    }
    close(STDOUT_FILENO);
    wait(NULL);
    fprintf(stderr, "%s terminated\n", argv[0]);
    exit(EXIT_SUCCESS);
}

上面的程序被称为

$ a.out -f output_file  command [args ...]

并将在command程序完成时结束。 试试看嘛

$ a.out -f out.txt echo a b c

你会得到

$ cat out.txt
a b c
$ _

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM