Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I read a FIFO/named pipe line by line from a C++/Qt Linux app?
    text
    copied!<p>How do I read a FIFO/named pipe line by line from a C++/Qt Linux app?</p> <p>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.</p> <p>Let's take a example with some shell commands to show what I would like the app to do.</p> <p>First create a fifo</p> <pre><code>mkfifo MyPipe </code></pre> <p>Then we can use cat to read from the fifo</p> <pre><code>cat MyPipe </code></pre> <p>And then we send some data in with another cat</p> <pre><code>cat &gt; MyPipe </code></pre> <p>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.</p> <p>Now the sender is easy to create with a QTextStream, you just need to flush when you want to send.</p> <pre><code>QFile file("MyPipe"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out(&amp;file); for(int i=0; i&lt;3; i++) { out &lt;&lt; "Hello...: " &lt;&lt; i &lt;&lt; "\n"; out.flush(); sleep(2); } file.close(); </code></pre> <p>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.</p> <p>Like this example:</p> <pre><code>QFile file("MyPipe"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return 0; QTextStream in(&amp;file); QString line; do { line = in.readLine(); qDebug() &lt;&lt; line; } while (!in.atEnd()); file.close(); </code></pre> <p>What am I missing?</p> <p>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...</p> <p>/Thanks</p> <hr> <p><em>Note</em>: </p> <p>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.</p> <pre><code>FILE *fp; fp=fopen("MyPipe", "r"); char c; while((c=getc(fp)) != EOF) { printf("%c",c); } fclose(fp); </code></pre> <hr> <p><strong>Update</strong>:</p> <p>When I start a debugger the program is hanging on the readLine(), and do not continue until the other party closes the fifo.</p> <p>And I do get the same using ">>"</p> <pre><code> line = in.readLine(); in &gt;&gt; line; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload