[fpc-pascal] TFileStream and stdin - how to make this work?

Andreas Schneider aksdb at gmx.de
Sun Oct 3 15:21:57 CEST 2010


On Sun, 03 Oct 2010 15:05:10 +0200, Bernd Kreuss
<prof7bit at googlemail.com> wrote:
> Hi,
> 
> this might be a stupid question but I find myself again struggling with
> file IO and stdin/stdout. I am trying to do the following:
> 
> StdIn := TFileStream.Create('/dev/stdin', fmOpenRead);
> 
> and later on i want to poll whether there is something to read. I want
> to try reading binary data that will be piped to my application and i
> need to poll (non blocking!) and in case of incoming data read the first
> byte to determine the type of message that has arrived and then read the
> rest.
> 
> The Stream seems to be able to read when there is something to read on
> stdin but I cannot find any way to actually test whether the ReadByte()
> will block (nothing to read) or whether there is data available.
> 
> the properties size and position both are always $ffffffffffffffff and
> the stream has no eof property.
> 
> Can this be done at all? And is my way of opening the stream correct?
> What would be a more platform independent way of opening the stdin as a
> stream to read binary data or is there a different and better way to do
> this, maybe a stream is not the correct thing for this at all? How do I
> poll and read binary data from stdin?
> 
> Bernd

You could use TIOStream from the unit iostream. AFAICS that should do
exactly what you want - i.e. opening stdin/out/err as a TStream
descendant.
I don't know however, if that fixes the problem you have with the
stream blocking when nothing is to be read. If nothing else helps I
guess you could use a thread to read the data from stdin, then it
shouldn't matter if it blocks.

Best regards,
Andreas.



More information about the fpc-pascal mailing list