简体   繁体   English

Fflush不写缓冲文本

[英]Fflush does not write buffered text

I'm trying to print a 2D table to my terminal using this code: 我正在尝试使用以下代码将2D表打印到我的终端:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/wait.h>

char getch()
{
    char ch;
    struct termios old, new;
    tcgetattr(0, &old); // grab old terminal i/o settings
    new = old; // new settings = old settings
    new.c_lflag &= ~ICANON; // disable buffered i/o
    new.c_lflag &= ~ECHO; //set echo mode
    tcsetattr(0, TCSANOW, &new); // use these new terminal i/o settings now
    ch = getchar();
    tcsetattr(0, TCSANOW, &old); //restore old settings
    return ch;
}

void readPBM(char *output)
{
    char tmp[1024];

    int fd[2] = {0,0};
    int pid;

    //Open the pipe for inter-process communication
    pipe(&fd[0]);

    //Fork and test if we are child or parent process
    pid = fork();
    if(pid) //Parent process
    {
        wait(NULL); //Wait for child's end
        close(fd[1]);//Close pipe's write stream
        close(0);//Close stdin
        dup(fd[0]);//Duplicate stdout
        close(fd[0]);//Close old stdout

        strcpy(output, "");// Init output at 0
        while(fgets(tmp, 1024, stdin) != NULL) //Put remaining entry in output
        {
            strcat(output, tmp);
        }
    }
    else if(pid == 0) //Child process
    {
        close(fd[0]);//Close pipe's read stream
        close(1);//Close stdout
        dup(fd[1]);//Duplicate stdin
        close(fd[1]);//Close old stdin

        printf("A random string ...\n");
    }
    else //Print error if fork failed
    {
        printf("Error creating a new process");
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char *argv[])
{
    int i, j;

    char *str = NULL;
    char c;

    str = malloc(512 * sizeof(char*));

    readPBM(str);

    printf("%s", str);
    fflush(stdout);
    c = getch();
}

I have a UNIX implementation of getch() . 我有getch()UNIX实现

My problem is that my program is waiting for an input to print the table. 我的问题是我的程序正在等待输入以打印表格。 I tried to fflush(stdout) and to disable terminal buffering with ICANON but it's still not working. 我尝试使用fflush(stdout)并使用ICANON禁用终端缓冲,但是仍然无法正常工作。

PS: it's not working neither with getchar, scanf, ... PS:getchar,scanf等都无法使用...

EDIT: So I came up to this minimal example; 编辑:所以我想出了这个最小的例子; it seems to be pipe-related. 它似乎与管道有关。

My problem is that my program is waiting for an input to print the table. 我的问题是我的程序正在等待输入以打印表格。

This is because you forgot to terminate the child process when it has done its work, so after printing A random string ... to the pipe it continues, returning from readPBM() and eventually executing the c = getch() at the end of main() . 这是因为您忘记了子进程完成工作后就终止了它,因此在A random string ...打印到管道之后,它继续进行,从readPBM()返回并最终在末尾执行c = getch() main() The remedy is to call exit(0) at the end of the if(pid == 0) //Child process block. 解决方法是在if(pid == 0) //Child process块的末尾调用exit(0)

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

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