File.In (gb)

Static Property Read In As File

Returns the standard input stream.

This example demonstrates how to read and write to standard input, output and error. It demonstrates the use of File.In, File.Out and File.Err. Each line seen on standard input is echoed back first on standard output and then on standard error.

Examples

PUBLIC SUB Main()
  DIM inputLine AS String
  ' Loop until the end of the standard input file stream
  WHILE NOT Eof(File.In)
    ' Read a line from standard input. This is the same as:
    ' LINE INPUT inputLine
    LINE INPUT #File.In, inputLine
    ' Print to standard output. This is the same as:
    ' PRINT inputLine
    PRINT #File.Out, inputLine
    ' Print to standard error
    PRINT #File.Err, inputLine
  WEND
END

Create a Command-line application and place the above code in the startup module. If you run this example in the Gambas IDE the application will appear to hang. If you enter some text in the IDE console window each line will be echoed back to you twice. But as the application never sees the end of file for the standard input stream you will need to stop the project.

To get a better idea of what the example is doing, create an executable from your project. Suppose we call this executable pipe.gambas. Open a Linux terminal and cd to the directory where you saved the executable. If you enter the command:

ls -a | ./pipe.gambas

you will see each line from the ls -a command echoed to you twice. Once from the standard output and once from standard error. This time this application will see the end of file from the standard input stream and so ends correctly. Now try:

ls -a | ./pipe.gambas > files.txt

here we redirect the standard output stream to a file and you will see the standard error output stream in the terminal window. You should now have a file in the directory with the output of the ls -a command.