如何从C ++/Qt Linux应用程序逐行读取FIFO/命名管道? [英] How do I read a FIFO/named pipe line by line from a C++/Qt Linux app?

查看:265
本文介绍了如何从C ++/Qt Linux应用程序逐行读取FIFO/命名管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C ++/Qt Linux应用程序逐行读取FIFO/命名管道?

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

今天,我可以从Qt程序的fifo中打开并读取内容, 但是我无法让程序逐行读取数据. Qt会读取整个文件,这意味着他要等到发件人"关闭其会话.

Today I can open and read from a fifo from a Qt program, but I can't get the program to read the data line by line. Qt reads the entire file, meaning he waits until the "sender" closes his session.

让我们以一些shell命令为例,以展示我希望应用程序执行的操作.

Let's take a example with some shell commands to show what I would like the app to do.

首先创建一个fifo

First create a fifo

mkfifo MyPipe

然后我们可以用cat读取fifo

Then we can use cat to read from the fifo

cat MyPipe 

然后我们与另一只猫一起发送一些数据

And then we send some data in with another cat

cat > MyPipe

然后开始输入内容,每当您按下Enter键时,它就会到达阅读器. 然后,当您使用Ctrl + D闭合时,两边都结束了.

And then start to type something, and every time you hit enter it arrives at the reader. And then when you close it with Ctrl+D both sides end.

现在,使用QTextStream可以轻松创建发件人, 您只需要在要发送时冲洗即可.

Now the sender is easy to create with a QTextStream, you just need to flush when you want to send.

QFile file("MyPipe");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    return;

QTextStream out(&file);
for(int i=0; i<3; i++) {
    out << "Hello...: " << i << "\n";
    out.flush();
    sleep(2);
}

file.close();

但是要写一个小小的阅读器,逐行阅读是我现在遇到的问题, 我对Qt lib的所有尝试最终都得到了我的数据,但是 直到发送者在fifo上使用file.close(). 不是当他冲水时发生的,就像我用猫来阅读时发生的那样.

But then to write a little reader that read line by line is where I'm stuck right now, all my tries with the Qt lib ends up with that I get the data but not until the sender uses file.close() on the fifo. Not when he flush, as occurs when I use cat to read.

像这个例子:

QFile file("MyPipe");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return 0;

QTextStream in(&file);
QString line;
do {
    line = in.readLine();
    qDebug() << line;
} while (!in.atEnd());


file.close();

我想念什么?

感觉就像我需要使用某种isReady或lineAvailable 溪流之类的东西 但我在文档中找不到适合的任何内容...

It just feels like I need to use some kind of isReady or lineAvailable on the stream or something like that, but I can't find anything in the docs that fits...

/谢谢

注意:

如果我使用低级c风格并在那时阅读一个char, 得到我正在寻找的风格. 但是能够执行相同的Qt样式会很好.

If I go with the low level c style and read one char at the time I do get the style Im searching for. But it would be nice to be able to do the same Qt style.

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


更新:

当我启动调试器时,程序挂在readLine()上, 并且除非另一方关闭fifo,否则不要继续.

When I start a debugger the program is hanging on the readLine(), and do not continue until the other party closes the fifo.

我确实使用">>"得到了相同的结果

And I do get the same using ">>"

    line = in.readLine();
    in >> line;

推荐答案

使用低级c样式并一次读取一个char.

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);

这篇关于如何从C ++/Qt Linux应用程序逐行读取FIFO/命名管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆