I’m sure that you all know by now how to drive your printers from a program; you simply replace your customary PRINT statement with a LPRINT statement.
Simple, right?
But what if you have a program which will print to the printer or to the screen at the user’s option? How do you program this ability into the computer? You might try something like this:
10 INPUT"Print to S-creen, or P-rinter";a$
20 IF a$="P" THEN LPRINT "Message"
30 IF a$="S" THEN PRINT "Message"
This method works well, but takes two sets of print statements at every output point. Thus taking up valuable memory and slowing your computerized masterpiece down.
Fortunately, Uncle Clive provided an alternate way of sending output to the screen and/or printer. This alternate way has to do with the way that your TS 2068/ZX Spectrum looks at outputted data.
Upon power-up, the computer sets up channels for your output to travel along. These channels are to the printer, screen, edit lines, and other areas beyond the scope of this article. It then assigns these channels to certain output statements: screen for PRINT and LIST; printer for LPRINT and LLIST; and edit lines for ROM output. This channeling of data is known as STREAMING. Each of the three primary output areas mentioned are given a number all their own. Stream 1 goes to the edit lines, stream 2 goes to the screen, and stream 3 goes to the printer. Thus; by changing the default area of each stream, we can redirect outputed data. This redirecting is accomplished by assigning the stream to a new area: “K” for the edit lines, “S” for the screen, and “P” for the printer. Thus, by changing the output area of stream 2, we can cause PRINT and LIST statements to go somewhere other than on the screen. This is done by using the OPEN# statement in the form of OPEN#<Stream>,"<Device>". Consider the new version of our program:
10 INPUT"Print to the S-creen, or P-rinter";a$:OPEN#2,a$
20 PRINT"Message"
This new version does the same as the first but shorter. In a longer program this can add up to a savings of several kilobytes. Of course, once you have re-routed the data, you can return it to its normal area by using the CLOSE# command in the format of CLOSE#<Stream>.
Another unique trick of streaming is the ability to print on the edit lines by opening stream 2 to the “K” device. See if you can figure this out from the previous examples we’ve used.