open (list-of-specifiers)
으로 많이 사용하는 규정자(specifier)로는:
[UNIT=] u
IOSTAT= ios
ERR= err
FILE= fname
STATUS= sta
ACCESS= acc
FORM= frm
RECL= rl
등이 있다. 장치 번호 u 는 file을 나타내는 9에서 99사이의 숫자 하나로 프로그램하는 사람이 임의로 택할 수 있지만 다른 곳에서는 사용하고 이지 않다는 것을 스스로 확인하여야한다.
ios 는 입출력 상태 표시로 정수 변수이다. 명령이 성공적으로 수행되었다면 ios가 0이고 아니면 0이 아니 어떤 값이 된다.
err 는 오류가 발생하면 이동할 문장 번호이다.
fname 은 file이름을 나타내는 문자열이다.
sta는 문자열로 NEW, OLD 또는 SCRATCH 중의 하나이다. 이는 file의 이전 상태를 나타낸다. scrath file은 만들어지고 file이 닫히거나 프로그램이 끝나면 지워지는 file이다.
acc 는 SEQUENTIAL 또는 DIRECT이어야만 한다. 미리 설정한 값은 SEQUENTIAL이다.
frm 은 FORMATTED 또는 UNFORMATTED이고 미리 설정한 값은 UNFORMATTED이다.
rl 은 direct-access file의 record의 길이를 나타낸다.
이런 specifiers에 대하여 더 자세히 알거 싶으면 Fortran 77 책을 참조하라.
file을 연 뒤에 read 또는 write 명령을 사용한다. file에 관한 일을 다 마치면 다음 명령을 사용하여 닫아야 한다.
close ([UNIT=]u[,IOSTAT=ios,ERR=err,STATUS=sta])
여늬 때와 마찬가지로 여기서 괄호 안의 변수는 선택적이다.
read ([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err, END=s)
write([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err, END=s)
대부분의 규정자는 앞에서 살펴 보았다. END=s는 file을 끝(end-of-file)에 다다르면 이동할 문장 번호를 나타낸다.
program inpdat
c
c This program reads n points from a data file and stores them in
c 3 arrays x, y, z.
c
integer nmax, u
parameter (nmax=1000, u=20)
real x(nmax), y(nmax), z(nmax)
c Open the data file
open (u, FILE='points.dat', STATUS='OLD')
c Read the number of points
read(u,*) n
if (n.GT.nmax) then
write(*,*) 'Error: n = ', n, 'is larger than nmax =', nmax
goto 9999
endif
c Loop over the data points
do 10 i= 1, n
read(u,100) x(i), y(i), z(i)
10 enddo
100 format (3(F10.4))
c Close the file
close (u)
c Now we should process the data somehow...
c (missing part)
9999 stop
end