Wednesday, June 18, 2014

fortran learning notes-3 file related operations

how to communicate with PC to open and operate files.

  1. open and write a file
example:

program test
implicit none
integer :: i
open(10, file=’cubes.dat’) ! 10 is the unit number of a file
do i=1,100
write(10,1) i, i*i, i**3
end do
close(10) ! do not forget to close it.
1 format(i4,i6,i8) ! see below for format explaination

For example, to read one 2-digit integer followed by two 8-digit integers, you could use the format statement:
10 format(i2,2i8)

2. open and read a file.
Codes in one example


open(unit=11,file="ch3oh-o2.xyz",status="old",action="read")
do i =1, np
read(11,*) atomsymbol(i),coordx(i),coordy(i),coordz(i)
end do
close(11)

No comments:

Post a Comment