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

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

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.

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

First create a fifo

mkfifo MyPipe

Then we can use cat to read from the fifo

cat MyPipe

And then we send some data in with another cat

cat > MyPipe

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.

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

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.

Like this example:

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

What am I missing?

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...

/Thanks

Note:

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

Update:

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;

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值