Siddy Sid
Siddy Sid

Reputation: 55

Opening a file on unit 5 or 6

I have a read/write operation going on in the Fortran code snippet as follows

OPEN(5,FILE='WKDAT.dat', STATUS='OLD')
OPEN(6,FILE='WKLST.dat', STATUS='UNKNOWN') 

I know that by default the unit number 5 is used for input from the keyboard and unit number 6 is used to display on the screen. Also I can use *.

But in the above-mentioned Fortran code unit number is 5 and a file name "WKDAT.dat" is given. So this means that the data is being read from "WKDAT.dat" file. Also there is code unit number 6 and a file name "WKLST.dat" is given. So this means that the data is being written to "WKLST.dat" file. Is my understanding correct?

As per my basic knowledge: Unit number 5 is only used to take input from keyboard & unit number 6 is only used to print to console so no files should be involved. But in the code snippet it has both unit number 5, 6 as well as file name.

So both are contradicting :(

In this link http://www.oc.nps.edu/~bird/oc3030_online/fortran/io/io.html they have mentioned the following "When I/O is to a file you must ASSOCIATE a UNIT number (which you choose) with the FILENAME. Use any unit number other than 5 and 6. On some computers, some unit numbers are reserved for use by the computer operating system."

Upvotes: 2

Views: 4014

Answers (3)

Túrin Turambar
Túrin Turambar

Reputation: 11

For older versions of FORTRAN they are special!
Here are some excerpts from a Sun Fortran compiler guide. (FORTRAN 77/95)

Preconnected Units
Three unit numbers are automatically associated with specific standard I/O files at the start of program execution. These preconnected units are standard input, standard output, and standard error:

Standard input is logical unit 5 (also Fortran 95 unit 100) Standard output is logical unit 6 (also Fortran 95 unit 101) Standard error is logical unit 0 (also Fortran 95 unit 102) Typically, standard input receives input from the workstation keyboard; standard output and standard error display output on the workstation screen.

In all other cases where a logical unit number but no FILE= name is specified on an OPEN statement, a file is opened with a name of the form fort.n, where n is the logical unit number.

Opening Files Without an OPEN Statement
Use of the OPEN statement is optional in those cases where default conventions can be assumed. If the first operation on a logical unit is an I/O statement other than OPEN or INQUIRE, the file fort.n is referenced, where n is the logical unit number (except for 0, 5, and 6, which have special meaning).

...

Importantly if you are Piping these in from a cmd line

Command-Line I/O Redirection and Piping
Another way to associate a physical file with a program's logical unit number is by redirecting or piping the preconnected standard I/O files. Redirection or piping occurs on the runtime execution command.

In this way, a program that reads standard input (unit 5) and writes to standard output (unit 6) or standard error (unit 0) can, by redirection (using <, >, >>, >&, |, |&, 2>, 2>&1 on the command line), read or write to any other named file.

So I can, in the command prompt, myProgram.exe < input.txt > output.txt
And use READ(5,#) to get the input.txt and likewise 6 for the output. So to conclude, they are special, yet I have never had any problem treating them like normal. So you can avoid using 0,5,6 if your worried, but I would not worry.

Link for reference
https://docs.oracle.com/cd/E19957-01/806-3593/2_io.html

Upvotes: 1

Nothing in the standard says units 5 and 6 have any special meaning although in practice standard input and standard output are often pre-connected to 5 and 6.

Module iso_fortran_env from Fortran 2008 contains constants

INPUT_UNIT
OUTPUT_UNIT
ERROR_UNIT

with the unit numbers where standard input, standard output and standard error are connected. These are allowed to be different than 5 and 6.

Opening a file in unit that is in use causes the unit to be associated with the new file.

For example the Cray Fortran manual says:

Unit numbers 100, 101, and 102 are permanently associated with the standard input, standard output, and standard error files, respectively.

That means if you open some other file as unit 5 or 6 standard input and standard output still have some other unit where they are pre-connected and they will not be closed.

Upvotes: 3

Ian Bush
Ian Bush

Reputation: 7434

Fortran has no magic unit numbers. The Fortran standard says nothing about 5, 6 or any other valid unit number being used for a special purpose. As such you are free to use the open statement to associate any valid unit number with a file. However traditionally for reasons that pre-date me 5 and 6 have been pre-associated with the keyboard and screen, as you say. Now still you can change the association by use of the open statement and that is fine save for the confusion it can cause, so most people I know recommend avoiding this and using unit numbers of 10 and upwards. Also because 5 and 6 are not guaranteed to be associated with the default input and output devices I would recommend against their use, preferring * or, in more modern code, the named constants input_unit, output_unit and error_unit from the iso_fortran_env intrinsic module.

So in summary you've got the right idea, and I'm not surprised you're confused.

Upvotes: 4

Related Questions