1
1
module stdlib_experimental_io
2
2
use iso_fortran_env, only: sp= >real32, dp= >real64, qp= >real128
3
+ use stdlib_experimental_error, only: error_stop
3
4
implicit none
4
5
private
5
- public :: loadtxt, savetxt
6
+ public :: loadtxt, savetxt, open
6
7
7
8
interface loadtxt
8
9
module procedure sloadtxt
@@ -46,7 +47,7 @@ subroutine sloadtxt(filename, d)
46
47
integer :: s
47
48
integer :: nrow,ncol,i
48
49
49
- open (newunit = s, file = filename, status = " old " , action = " read " )
50
+ s = open (filename )
50
51
51
52
! determine number of columns
52
53
ncol = number_of_columns(s)
@@ -89,7 +90,7 @@ subroutine dloadtxt(filename, d)
89
90
integer :: s
90
91
integer :: nrow,ncol,i
91
92
92
- open (newunit = s, file = filename, status = " old " , action = " read " )
93
+ s = open (filename )
93
94
94
95
! determine number of columns
95
96
ncol = number_of_columns(s)
@@ -132,7 +133,7 @@ subroutine qloadtxt(filename, d)
132
133
integer :: s
133
134
integer :: nrow,ncol,i
134
135
135
- open (newunit = s, file = filename, status = " old " , action = " read " )
136
+ s = open (filename )
136
137
137
138
! determine number of columns
138
139
ncol = number_of_columns(s)
@@ -164,7 +165,7 @@ subroutine ssavetxt(filename, d)
164
165
! call savetxt("log.txt", data)
165
166
166
167
integer :: s, i
167
- open (newunit = s, file = filename, status = " replace " , action = " write " )
168
+ s = open ( filename, " w " )
168
169
do i = 1 , size (d, 1 )
169
170
write (s, * ) d(i, :)
170
171
end do
@@ -187,7 +188,7 @@ subroutine dsavetxt(filename, d)
187
188
! call savetxt("log.txt", data)
188
189
189
190
integer :: s, i
190
- open (newunit = s, file = filename, status = " replace " , action = " write " )
191
+ s = open ( filename, " w " )
191
192
do i = 1 , size (d, 1 )
192
193
write (s, * ) d(i, :)
193
194
end do
@@ -210,7 +211,7 @@ subroutine qsavetxt(filename, d)
210
211
! call savetxt("log.txt", data)
211
212
212
213
integer :: s, i
213
- open (newunit = s, file = filename, status = " replace " , action = " write " )
214
+ s = open ( filename, " w " )
214
215
do i = 1 , size (d, 1 )
215
216
write (s, * ) d(i, :)
216
217
end do
@@ -268,4 +269,32 @@ logical function whitechar(char) ! white character
268
269
end if
269
270
end function
270
271
272
+ integer function open (filename , mode ) result(u)
273
+ ! Open a file
274
+ !
275
+ ! To open a file to read:
276
+ !
277
+ ! u = open("somefile.txt") # The default `mode` is "r"
278
+ ! u = open("somefile.txt", "r")
279
+ !
280
+ ! To open a file to write:
281
+ !
282
+ ! u = open("somefile.txt", "w")
283
+
284
+ character (* ), intent (in ) :: filename
285
+ character (* ), intent (in ), optional :: mode
286
+ character (:), allocatable :: mode_
287
+ mode_ = " r"
288
+ if (present (mode)) mode_ = mode
289
+ ! Note: the Fortran standard says that the default values for `status` and
290
+ ! `action` are processor dependent, so we have to explicitly set them below
291
+ if (mode_ == " r" ) then
292
+ open (newunit= u, file= filename, status= " old" , action= " read" )
293
+ else if (mode_ == " w" ) then
294
+ open (newunit= u, file= filename, status= " replace" , action= " write" )
295
+ else
296
+ call error_stop(" Unsupported mode" )
297
+ end if
298
+ end function
299
+
271
300
end module
0 commit comments