Input and Output

The WScript object has three special properties for working with the standard input, output, and error streams. These properties are: These properties return text stream objects. The properties and methods of these objects are:
Wscript.StdIn
MethodDescription
Close() Closes a text stream.
Read() Takes an integer number as an argument and returns the specified number of characters from an input stream.
ReadAll() Returns all characters from an input stream. The ReadAll method returns a string.
ReadLine() Reads one line from the standard input. This method also returns a string value.
Skip() Takes an integer number as an argument and skips the specified number of characters when reading from an input text stream. The position pointer moves forward by the number of characters (bytes) specified in the argument. You cannot use the Skip method to skip backwards through a file (negative character values are not supported). The Skip method is limited to the open for reading mode only (you cannot skip a specified number of characters when writing to an output stream).
SkipLine() Skips the next line when reading from an input text stream. (The same restrictions as above are applied.)
PropertyDescription
AtEndOfLine Returns a Boolean value indicating whether the end of a line in an input stream has been reached.
AtEndOfStream Returns a Boolean value indicating whether the end of an input file (stream) has been reached.
Column Returns the column number of the current character position in an input stream.
Line Returns the current line number in an input stream. After a stream is first opened, Line will initially be 1.
Wscript.StdOut and  WScript.StdErr
MethodDescription
Close() Closes a text stream.
Write() Takes a string as an argument and sends it to an output stream.
WriteBlankLine()   Takes an integer number as an argument and sends the specified number of blank lines (newline characters) to an output stream. WScript.StdOut.WriteLine() is equivalent to WScript.StdOut.WriteBlankLine(1)
WriteLine() Sends a string with a newline character to an output stream. If a string argument is omitted only the new line character is sent to the output stream.
The properties StdIn, StdOut, and StdErr work when running the script with the CScript host executable file only. An "Invalid Handle" error is returned when run with WScript.

In the following example we read only first ten characters from each line and then print them into the standard output:

var str = "";
while( ! WScript.StdIn.AtEndOfStream  ){
  str = WScript.StdIn.Read(10); // read 10 characters from the STDIN
  WScript.StdIn.SkipLine();
  WScript.StdOut.WriteLine(str);
}
Actually this algorithm works fine only if all the strings have more that 10 characters in it. If there is a string with less than 10 characters it fails. In the newer version we will read character by character until we read the 10-th character or reach the end of a line:
var str;
var inp = WScript.StdIn;
while( ! inp.AtEndOfStream ){
  str = "";
  while( inp.Column<=10 && !inp.AtEndOfLine && !inp.AtEndOfStream )
    str += inp.Read(1);
  if( ! inp.AtEndOfStream )
    inp.SkipLine(); // skip till the end of the line
  WScript.StdOut.WriteLine(str);
}

In-class assignment

  1. Write a script that skips 10 characters from each line and then reads the next 5 characters or till the end of the line (whichever comes first), converts the characters to a number and sums up all these numbers. The output of the script should be the total sum. For example, if user entered:
    Nothing   12    some other text
    Less      4     comments
    TVset     126
    Ticket    10
    
    It should print 152.
  2. Write a script that reads line by line from the standard input and prints lines with even numbers into the standard output and lines with the odd numbers into the error stream. Please also reverse the lines printed to the error stream. That is, instead of "Bobby Fisher" you should print "rehsiF ybboB". To reverse a line create a function reverse (use method substr) of a string object to extract characters from a string variable. This method takes two arguments - the position where to start the substring and how many characters from a string include to the substring. For example,
    var str = "Test ";
    WScript.Echo( str.substr(1,2) ); // will print "es" - position numbers start with zero