C 从文件中只打印第一行。使用叉子和管子

C 从文件中只打印第一行。使用叉子和管子,c,pipe,C,Pipe,我正在尝试打印一个文件的多行,其中空格替换为*并将所有内容转换为大写。这应该通过管道和叉子来完成。为什么只打印第一行 void writer(int inpipe) { char read_msg[BUFFER_SIZE]; pid_t pid; int fd[2]; read(inpipe, read_msg, BUFFER_SIZE); printf("%s\n",read_msg); return; } void p2(int inpipe

我正在尝试打印一个文件的多行,其中空格替换为*并将所有内容转换为大写。这应该通过管道和叉子来完成。为什么只打印第一行

void writer(int inpipe)
{
    char read_msg[BUFFER_SIZE];
    pid_t pid;
    int fd[2];
    read(inpipe, read_msg, BUFFER_SIZE);
    printf("%s\n",read_msg);
    return;
}

void p2(int inpipe)
{
    char read_msg[BUFFER_SIZE];

    pid_t pid;
    int fd[2];
    if(pipe(fd) == -1) {
        perror("Pipe error");
        return;
    }

    pid = fork();
    if(pid<0) {  //error
        perror("Fork Failed");
        return;
    }
    else if(pid==0) { //child
        close(fd[1]);
        writer(fd[0]);
        close(fd[0]);
        return;
    }
    else {  //parent, p2()
        read(inpipe, read_msg, BUFFER_SIZE);
        int i = 0;
        while (read_msg[i] != '\0') {
            read_msg[i] = toupper(read_msg[i]);
            i++;
        }
        close(fd[0]);
        write(fd[1],read_msg,(unsigned long)(strlen(read_msg)+1));
        close(fd[1]);
    }
    return;
}

void p1(int inpipe)
{
    char read_msg[BUFFER_SIZE];

    pid_t pid;
    int fd[2];
    if(pipe(fd) == -1) {
        perror("Pipe error");
        return;
    }

    pid = fork();
    if(pid<0) {  //error
        perror("Fork Failed");
        return;
    }
    else if(pid==0) { //child
        close(fd[1]);
        p2(fd[0]);
        close(fd[0]);
        return;
    }
    else {  //parent, p1()
        read(inpipe, read_msg, BUFFER_SIZE);
        int i = 0;
        while (read_msg[i] != '\0') {
            if ((read_msg[i] == ' '))
                read_msg[i] = '*';
            i++;
        }
        //printf("%s\n",read_msg);
        close(fd[0]);
        write(fd[1],read_msg,(unsigned long)(strlen(read_msg)+1));
        close(fd[1]);
    }
    return;
}

int main(int argc, char **argv) {
    pid_t pid;
    int fd[2];

    if(pipe(fd) == -1) {
        perror("Pipe error");
        return 1;
    }
    pid = fork();
    if(argv[1] != NULL) {
        char const* const fileName = argv[1];
        FILE* file = fopen(fileName, "r");
        char line[BUFFER_SIZE];

        while (fgets(line, sizeof(line), file)){
            if(pid<0) {  //error
                perror("Fork Failed");
                return 1;
            }
            else if(pid==0) { //child
                close(fd[1]);
                p1(fd[0]);
                close(fd[0]);
            }
            else {  //parent, main(), reader
                //reading from the file and put it into the pipe[1]
                close(fd[0]);
                write(fd[1],line,strlen(line)+1);
                close(fd[1]);
            }
        }
    }
    else
        printf("No file detected.\n");

    return EXIT_SUCCESS;
}
void编写器(int-inpipe)
{
字符读取消息[缓冲区大小];
pid_t pid;
int-fd[2];
读取(管道、读取消息、缓冲区大小);
printf(“%s\n”,读取消息);
回来
}
无效p2(管道内部)
{
字符读取消息[缓冲区大小];
pid_t pid;
int-fd[2];
如果(管道(fd)=-1){
perror(“管道错误”);
回来
}
pid=fork();

如果(很遗憾,您需要重新设计。在
fork()之前的
main()中打开管道一次
,并且每次从文件中读取一行代码时,它们都会尝试关闭它们。如果您像应该的那样检查系统调用的返回,您将看到左侧、右侧和中间的失败。但是,如果您不关闭它们,您的
读取()的方式
编写的调用将使他们读得太多,并丢弃您需要的输入。您永远不会为您的孩子等待()
。不幸的是,此代码无法挽救,您需要一个完全不同的策略。更不用说,在第一次迭代中,您已经有两个进程,并且通过
p1()
您的孩子最终创建了两个以上的进程。这些进程都不会终止,因此它们将返回并返回循环。其中三个进程将分别在文件的第二行创建另外两个进程,因此您将有10个。其中九个进程将在第三行创建另外两个进程,因此您将有28个。即使它起作用了,在你自己的系统中打印每一行成千上万次之前,你也不会在文件中走得太远。