Problems using STDIN

BlitzMax Forums/BlitzMax Programming/Problems using STDIN

doswelk(Posted 2006) [#1]
I have compiled the code below into STDINTEST.EXE, and when I try to Pipe anything through it, it just siezes up.

The command that is actually causing the problem is ReadStdin(), I thought initially I was going into an endless loop during While...Wend, but this is not the case.

Any idea where I am going wrong?

Test it from a command line like this:
C:\Blitz\stdin> Echo Hello | stdintest.exe

*****
WARNING: This code will hang.
Run it in a command window, and you may need to terminate the process manually.
*****

Framework BRL.StandardIO

Print "POS=" + StandardIOStream.Pos()
Print "IO=" + StandardIOStream.Size()
Print "EOF=" + Eof(StandardIOStream)

While Not Eof(StandardIOStream)
' THIS LINE CAUSES THE APP TO HANG
Print "=='" + ReadStdin() + "'=="
Wend

Print "STDIN done"


Dreamora(Posted 2006) [#2]
You know that IO commands are "locking" commands. thsi means if you read something with stdin and there isn't anything, you will have quite some time to wait as the whole app freezes at that point.


doswelk(Posted 2006) [#3]
OK, so that explains why it locks up; so how do I check if there is something there to actually get?

I would have expected WHILE NOT EOF(StandardIOstream)...WEND would have done this.

Is it locking because I need to somehow set StandardIOStream to INPUT?


marksibly(Posted 2006) [#4]
StandardIO is a 'pure', non-seekable stream so Pos() and Size() have no meaning.

StandardIO will also never reach end-of-file.

ReadStdin blocks until an end-of-line is received, so you'll have to pipe something with an end-of-line in it instead of just 'hello'.


Fabian.(Posted 2006) [#5]
I wrote a module containing an alternative stdio stream object. The problem is it wouldn't make sense to post the code here, since like in its current version you would need three more module from my scope just to install this one. Sadly it's win32 only.

fmc.StdIOStream

The module buffers the input data, so if there's still data in this buffer your application won't lock; you can get the buffer's size with StreamSize ( StdIOStream ( ) ); Eof ( StdIOStream ( ) ) returns True as soon as the buffer is empty and won't be filled any more. If your application is started like "Echo Hello | yourapp" it returns true if "Echo Hello" is terminated; If your app is started like "yourapp < filename" it returns true if all contents of the file are read.
Strict
Framework brl.blitz

Import fmc.StdIOStream

While Not Eof ( StdIOStream ( ) )
  Print "==" + ReadLine ( StdIOStream ( ) ) + "=="
Wend
works with this module - sorry, it would be easilier if it would be an import file or something like this or cross-plattform, but till now it wasn't possible for me to solve this... :-(