-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_file.f90
93 lines (68 loc) · 2.78 KB
/
open_file.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module open_file
!
! Purpose:
! This module contains routines to open files smoothly
!
! Date Author History of Revison
! ==== ====== ==================
! 18.02.2014 Svenja M. Janke Original
! Sascha Kandratsenka
! Dan J. Auerbach
interface
subroutine open_for_read(lun, file_name)
integer, intent(in) :: lun
character, intent(in) :: file_name
end subroutine
subroutine open_for_write(lun, file_name)
integer, intent(in) :: lun
character, intent(in) :: file_name
end subroutine
subroutine open_for_append(lun, file_name)
integer, intent(in) :: lun
character, intent(in) :: file_name
end subroutine
! contains module procedure
endinterface
contains
end module
subroutine open_for_read(lun,file_name)
implicit none
integer, intent(in) :: lun
character(len=*), intent(in) :: file_name
integer :: ios
character(len=120) :: error_message
open(unit=lun, file=file_name, status='old', action='read', iostat=ios, iomsg=error_message)
if (ios==0) return
print '(/ "Error on open file ", (a), " for read i/o status=", i4 )', TRIM(file_name), ios
print '( "error message=", (a) )', error_message
STOP 101
end subroutine open_for_read
subroutine open_for_write(lun,file_name)
implicit none
integer, intent(in) :: lun
character(len=*), intent(in) :: file_name
integer :: ios
character(120) :: error_message
open(unit=lun, file=file_name, status='new', action='write', iostat=ios, iomsg=error_message)
if (ios==0) return
open(unit=lun, file=file_name, status='replace', action='write', iostat=ios, iomsg=error_message)
if (ios==0) return
print *, 'failed to open file ', file_name, ' for write with status=replace. i/o status =',ios
print *, 'error message: ', error_message
STOP 103
end subroutine open_for_write
subroutine open_for_append(lun,file_name)
implicit none
integer, intent(in) :: lun
character(len=*), intent(in) :: file_name
integer :: ios
character(120) :: error_message
open(unit=lun, file=file_name, status='new', action='write', iostat=ios, iomsg=error_message)
if (ios==0) return
open(unit=lun, file=file_name, status='old', access='append', action='write', &
iostat=ios, iomsg=error_message)
if (ios==0) return
print *, 'failed to open file ', file_name, ' for write with status=old. i/o status =',ios
print *, 'error message: ', error_message
STOP 103
end subroutine open_for_append