views:

169

answers:

3

Hi

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;
+1  A: 

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 ?

shodanex
strace is nice, and I started a "watch tail reader.log" in another window and I can see that he do gets the data I send to him.
Johan
@Johan : are you sure the problem is on the reader side ?
shodanex
95% sure since if I replace the "Qt version" with the "C version" it works.
Johan
@Johan : Maybe QtextStream is the suspect then
shodanex
A: 

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

Edgar Bonet
God idea, but it's still the same :(
Johan
A: 

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