Output a text to the standard printer

Hello,
I would like to read the contents of a text file and output the text line by line to the standard printer (OS = WINDOWS 11)
Compiler: gfortran, ifort
Unfortunately, I couldn’t find a corresponding command, function or example code on the Internet.
Kind Regards
Juergen

What do you mean by “the standard printer” ? The default printer of Windows ? Or the standard output in the terminal ?

I believe at one time one could just open a file named “prn” or “lpt1” and write to it:

open(file=‘prn’,unit=6)
write(6,*) ‘hello,world’

but apparently it is harder now. See:
https://www.frazerhelp.com/help-manual/lpt-ports-and-windows-10.htm

Hello,
I mean naturally the default printer which is connected to the WINDOWS PC via cable or WLAN.
The output on the terminal is easy, there is the “write” or “print” command for that.
Kind Regards
Juergen

Hello,
thank you for your fast answer.
with:

open(file='lpt1',unit=6) ! line 8
write(6,*) 'Hello, World!'

I get the following message:

At line 8 of file print.f90 (unit = 6)
Fortran runtime error: Cannot open file 'lpt1': Permission denied
Error termination. Backtrace:
Could not print backtrace: libbacktrace could not find executable to open
#0 0xf3dc04ba
#1 0xf3db7e21
#2 0xf3db3178
#3 0xf3dbbd07
#4 0xf3db3da7
#5 0xf3d91769
#6 0xf3d917ff
#7 0xf3d913ad
#8 0xf3d914e5
#9 0x1b1c26ac
#10 0x1c12aa67
#11 0xffffffff

I can print a test-page.
Kind Regards
Juergen

There is no standard way to do this. Intel Fortran supports an extension DISPOSE=‘PRINT’ in its OPEN and CLOSE statements, but the documentation says:

On Windows*, PRINT and the PRINT part of PRINT/DELETE do not use the system PRINT command. One of the following occurs:

  • If you set the environment variable FOR_DEFAULT_PRINT_DEVICE to a print device, the Fortran runtime will copy the file to that device.
  • Otherwise, the Fortran runtime will copy the file to a new file named “PRN-FILE”. In this case, you will need to do the actual printing

While you could associate a printer with the LPT1 device with the net use command, and then issue a print command, but that may or may not be useful.

If you’re using Intel Fortran, there is a worked sample called WinPrint in the Miscellaneous folder of the samples bundle that allows you to print a text file directly to any defined printer. There is also WinPrintDirect to print non-text files that the printer may understand, such as .PS or .PRN.

Hoever ugly, you could do this:

program print
call execute_command_line(“notepad/p output.txt”)
end program print

Microsoft doesn’t like you using Fortran. They defend their patch every way they can.

This is precisely why I wanted you to disambiguate your question: to avoid any long answer about printers in the case your question would have had a straightforward answer about the terminal (and I can’t know if you are a beginner or not).

This is indeed a pragmatic approach…

This issue would be the same with any other language that is not printer-aware.

Hello,
thanks for the Fortran code “print”.

program print
call execute_command_line('notepad/p output.txt')
end program print

It works like this, I think the “notepad” program needs to be installed.
Kind Regards
Juergen

If you have a Windows box, notepad is already installed.

1 Like