How do I read a FIFO/named pipe line by line from a C++/Qt Linux app?

青春壹個敷衍的年華 提交于 2019-11-30 09:42:03

Use the low level c style and read one char at the time.

FILE *fp;
fp=fopen("MyPipe", "r");
char c;
while((c=getc(fp)) != EOF)
{
    printf("%c",c);
}
fclose(fp);

I don't know what does not work but you can try to debug it using strace:

strace -o writer.log -e trace=write ./writer 
strace -o reader.log -e trace=read ./reader 

The first line will log all the write system call made by your writer program. The second line works in a similar fashion. This way you can trace the system call, and be sure that your flushing works.

If you see repeated call to read, with the correct timing and data, then you have a problem with QTextStream.

What happens if you don't use a QTextStream, but directly read from the file ?

Yogi Joshi
if(file.bytesAvailable())
QString line = file.readLine();

You could use something like this.

You could try to open the file on the reader side with the QIODevice::Unbuffered flag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!