-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrewind.f90
44 lines (44 loc) · 1.17 KB
/
rewind.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
program main
use iso_fortran_env, only: iostat_end
implicit none
integer :: iu,i,ierr,j
character (len=*), parameter :: fmt_i = "(i0)"
character (len=100) :: text
open (newunit=iu,file="temp.txt")
write (iu,fmt_i) (i,i=1,3)
rewind (iu) ! move position to beginning of file
write (iu,fmt_i) (2*i,i=1,3)
! deletes previous data and writes 2 4 6 on successive lines
! position is now after last line written
backspace (iu) ! move file position to last line written
read (iu,*) j
print*,j ! 6
backspace (iu) ; backspace (iu) ; backspace (iu)
read (iu,*) j
print*,j ! 2
read (iu,*) ! skip a line -- move file position by 1 line
read (iu,*) j
print*,j ! 6
rewind (iu)
write (iu,"(i0/i0/a)") 10,20,"abc"
rewind (iu)
do
read (iu,*,iostat=ierr) j
if (ierr == 0) then
print*,"j =",j
else if (ierr == iostat_end) then
stop "reached end of file"
else
backspace (iu)
read (iu,"(a)") text
stop "could not read integer from '" // trim(text) // "'"
end if
end do
end program main
! output:
! 6
! 2
! 6
! j = 10
! j = 20
! STOP could not read integer from 'abc'