Skip to main content

3.4) What is the Unit Number


In the open statement open(unit=22, file=’inputdata.dat’), the unit=22 is a label. It is a nickname for the file. You decide the number and it is common practice that the number is between about 10 and 100. Some lower numbers are reserved. If you are linking up more than one file then use different unit numbers for each. So if you want to read from the inputdata.dat file then you just need to refer to the number 22 (as the nickname) to do it.
When you read from a file, instead of using read *, which reads a value from the keyboard, now you use read(22,*) to do it and the value 22 is the link that tells it to read from that specific file (in this case inputdata.dat).
On the next page is an example to show you open in action in a program.  The code reads in a value of a and outputs the value to the screen.