Hello world/Line printer

From Rosetta Code
Jump to: navigation, search
Task
Hello world/Line printer
You are encouraged to solve this task according to the task description, using any language you may know.

Cause a line printer attached to the computer to print a line containing the message Hello World!

Note: A line printer is not the same as standard output. A line printer was an older-style printer which prints one line at a time to a continuous ream of paper. With some systems, a line printer can be any device attached to an appropriate port (such as a parallel port).

Contents

[edit] Ada

[edit] Unix

Assuming that the line printer is attached to /dev/lp0

 
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Print_Line is
Printer : File_Type;
begin
begin
Open (Printer, Mode => Out_File, Name => "/dev/lp0");
exception
when others =>
Put_Line ("Unable to open printer.");
return;
end;
 
Set_Output (Printer);
Put_Line ("Hello World!");
Close (Printer);
end Print_Line;
 

[edit] Applesoft BASIC

Assumes a printer card is installed in the Apple II's number 1 expansion slot.

 
PR#1
PRINT "HELLO WORLD!"
 

[edit] AutoHotkey

 
Fileappend, Hallo World!, print.txt
Run, print "print.txt"
 

[edit] AWK

 
BEGIN { print("Hello World!") >"/dev/lp0" }
 

[edit] BASIC

Works with: QBasic
Works with: ZX Spectrum Basic
Works with: Liberty BASIC
LPRINT "Hello World!"

[edit] Batch File

ECHO Hello world!>PRN

[edit] BBC BASIC

      prn% = OPENOUT("PRN:")
PRINT #prn%, "Hello World!"
CLOSE #prn%

[edit] C

[edit] Unix

Assuming that the line printer is attached to /dev/lp0

#include <stdio.h>
 
int main()
{
FILE *lp;
lp = fopen("/dev/lp0","w");
fprintf(lp,"Hello world!\n");
fclose(lp);
return 0;
}

[edit] C++

#include <iostream>
#include <fstream>
 
int main(){
std::ofstream lprFile;
lprFile.open( "/dev/lp0" );
lprFile << "Hello World\n";
lprFile.close();
return 0;
}

[edit] COBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. GOODBYE-WORLD-PRINTER.
 
PROCEDURE DIVISION.
DISPLAY 'Hello World!'
UPON PRINTER
END-DISPLAY.
STOP RUN.

[edit] D

import std.stdio;
 
void main()
{
auto lp = File("/dev/lp0", "w");
lp.writeln("Hello World!");
}
 

[edit] Delphi

program Project1;
 
{$APPTYPE CONSOLE}
 
uses Printers;
 
var
lPrinterAsTextFile: TextFile;
begin
AssignPrn(lPrinterAsTextFile);
Rewrite(lPrinterAsTextFile);
Writeln(lPrinterAsTextFile, 'Hello World!');
CloseFile(lPrinterAsTextFile);
end.

[edit] Factor

Prints through Unix "lpr" command.

( scratchpad ) USE: io.encodings.utf8
( scratchpad ) USE: io.launcher
( scratchpad ) "lpr" utf8 [ "Hello World!" print ] with-process-writer

[edit] Go

package main
 
import (
"fmt"
"os"
)
 
func main() {
lp0, err := os.Create("/dev/lp0")
 
if err != nil {
panic(err)
}
 
defer lp0.Close()
 
fmt.Fprintln(lp0, "Hello world!")
}

[edit] Groovy

new File('/dev/lp0').write('Hello World\n')
 

[edit] GUISS

Start,Programs,Accessories,Notepad,Type:Goodbye World[pling],
Menu:File,Print,Button:OK

[edit] Haskell

 
import System.Cmd
 
cmd = "echo \"Hello World!\" | lpr"
 
main = system cmd
 

[edit] Integer BASIC

See Applesoft BASIC.

[edit] J

require'print'
print'Hello world!'

[edit] Java

import java.io.FileWriter;
import java.io.IOException;
 
public class LinePrinter {
public static void main(String[] args) {
try {
FileWriter lp0 = new FileWriter("/dev/lp0");
lp0.write("Hello World!");
lp0.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

[edit] JavaScript

// This example runs on Node.js
var fs = require('fs');
// Assuming lp is at /dev/lp0
var lp = fs.openSync('/dev/lp0', 'w');
fs.writeSync(lp, 'Hello, world!\n');
fs.close(lp);

[edit] Locomotive Basic

10 PRINT #8, "Hello World!"

[edit] Mathematica

commandstring = "echo Hello World!  | lpr -P Printer01"
Run[commandstring]

[edit] MATLAB / Octave

[edit] Unix

Assuming that the line printer is attached to /dev/lp0

  fid = fopen('/dev/lp0'); 
fprintf(fid,'Hello World!\n');
fclose(fid);

[edit] OCaml

Assuming that the line printer is attached to /dev/lp0

let () =
let oc = open_out "/dev/lp0" in
output_string oc "Hello world!\n";
close_out oc ;;

[edit] OpenEdge/Progress

OUTPUT TO PRINTER.
PUT UNFORMATTED "Hello world!" SKIP.
OUTPUT CLOSE.

[edit] Pascal

Works with: Free_Pascal
Library: Printer

Example from the FreePascal documentation:

program testprn;
uses printer;
var i: integer;
f: text;
begin
writeln ( 'Test of printer unit' );
writeln ( 'Writing to lst ...' );
for i := 1 to 80 do
writeln ( lst, 'This is line', i, '.' #13 );
close ( lst );
writeln ( 'Done.' );
{$ifdef Unix }
writeln ( 'Writing to pipe ...' );
assignlst ( f, '|/usr/bin/lpr −m' );
rewrite ( f );
for i:= 1 to 80 do
writeln ( f, 'This is line ', i, '.'#13 );
close ( f );
writeln ( 'Done.' )
{$endif}
end.

[edit] Perl

Assuming that the line printer is attached to /dev/lp0

open O, ">", "/dev/lp0";
print O "Hello World!\n";
close O;

[edit] Perl 6

given open '>', '/dev/lp0' {
.say('Hello, World!');
.close;
}

[edit] PHP

<?php
file_put_contents('/dev/lp0', 'Hello world!');
?>
<?php
fclose(STDOUT);
$STDOUT = fopen('/dev/lp0', 'a');
echo 'Hello world!';
?>

[edit] PicoLisp

(out '(lpr "-P" "Printer01")
(prinl "Hello world") )

[edit] PL/I

 
hello: procedure options (main);
put ('Hello world.');
end hello;
 

[edit] PostScript

Technically not really correct as this has to be sent to the printer directly. It will output Hello world, then, though.

<</PageSize [595 842]>> setpagedevice  % set page size to DIN A4
/Courier findfont  % use Courier
12 scalefont setfont  % 12 pt
28 802 moveto  % 1 cm from the top and left edges
(Hello world) show  % draw the string

[edit] PureBasic

Library: PureLPRINT
MyPrinter$ = LPRINT_GetDefaultPrinter()
If LPRINT_OpenPrinter(MyPrinter$)
If LPRINT_StartDoc("Printing a RC-Task")
LPRINT_Print(Chr(27) + "E") ; PCL reset for HP Printers
LPRINT_PrintN("Hello World!")
LPRINT_NewPage()
LPRINT_EndDoc()
EndIf
LPRINT_ClosePrinter()
EndIf

[edit] Python

Assuming that the line printer is attached to /dev/lp0:

lp = open("/dev/lp0")
lp.write("Hello World!\n")
lp.close()

If the above code gives you the error "IOError: File not open for writing", try:

lp = open("/dev/lp0","w")
lp.write("Hello World!\n")
lp.close()

[edit] Racket

 
#lang racket
(define (print text)
 ;; try lpr first
(define lpr-exe (find-executable-path "lpr"))
 ;; otherwise use a special file
(if lpr-exe
(with-input-from-string (~a text "\n") (λ() (void (system* lpr-exe))))
(with-output-to-file #:exists 'append
(case (system-type) [(windows) "PRN"] [else "/dev/lp0"])
(λ() (displayln text)))))
(print "Hello World!")
 

[edit] REXX

There is no direct way for REXX programs to write to the printer, but a shell command could be used.

In DOS (or under Windows):

str='Hello World'
'@ECHO' str ">PRN"

[edit] RPG

Works with: ILE RPG
 
Fqsysprt O F 80 printer
C except
C seton LR
Oqsysprt E
O 11 'Hello world'
 

[edit] Ruby

Assumes that lpr command reaches printer.

open("| lpr", "w") { |f| f.puts "Hello World!" }

[edit] Run BASIC

 shell$("echo \"Hello World!\" | lpr")

[edit] Salmon

Assuming /dev/lp0 accesses the printer:

open_output_text_file("/dev/lp0").print("Hello World!");

Assuming lpr is a command that prints to a printer:

`echo "Hello World!" | lpr`;

[edit] Scheme

[edit] Unix

Assuming device is attached to lp0

(call-with-output-file "/dev/lp0"
  (lambda (printer)
    (write "Hello World!" printer)))

[edit] SNOBOL4

In SNOBOL4, variables can be associated with input and output files. Assigning a value to an output-associated variable also writes it to the associated output file. (Likewise, accessing a variable associated with an input file returns as its value the next record from the associated input file.) By default, the variable "input" is associated with standard input, and the variable "output" is associated with standard output.

     output = "Hello, world."

You can associate the variable "print" with lpt1 (the default local printer port) using the output() function:

     output(.print,25,"lpt1")
print = "Hello, world."

[edit] Tcl

[edit] Unix

exec lp << "Hello World!"
set f [open |lp w]
puts $f "Hello World!"
close $f

[edit] Windows

set f [open prn w]
puts $f "Hello World!"
close $f

[edit] UNIX Shell

Use one of the following lines.

# Use the default printer queue, with lp(1) or lpr(1).
# 1. The system must have a printer queue.
# 2. The printer queue must understand plain text.
# 3. System V has lp(1). BSD has lpr(1).
# CUPS has both lp(1) and lpr(1).
#
echo 'Hello World!' | lp
echo 'Hello World!' | lpr
 
# Use a character device.
# 1. The device must understand plain text.
# 2. You must have write permission for the device.
# 3. Some systems have /dev/lp0, /dev/lp1, ...
# 4. BSD has /dev/lpt0, /dev/lpt1, ... for the parallel ports;
# and /dev/ulpt0, /dev/ulpt1, ... for the USB printers.
# Note that intermingling can occur if two processes write to the device at the
# same time. Using the print spooler method above avoids this problem,
#
echo 'Hello World!' >/dev/lp0
echo 'Hello World!' >/dev/lpt0
echo 'Hello World!' >/dev/ulpt0

[edit] XPL0

code Text=12;
Text(2, "Hello World!
");

The 2 directs the output to the printer (LPT1). Output is usually directed to the console using device code 0 instead.

A carriage return and line feed are normally required to make a line printer actually print. (A laser or inkjet printer may require a form feed.) However, some printers, or printer drivers, have a timeout feature that print even without the CR+LF (or FF). The CR+LF can simply be included in the string as shown. Another method is to include the CR+LF control characters as ^M^J.

Personal tools
Namespaces

Variants
Actions
Community
Explore
Misc
Toolbox