If the file can not be opened with the mode, the program aborts. If this err argument is given, .true. is substituted to err and -1 is substituted to unit and the program does not abort.
196 implicit none
197 integer, intent(out):: unit
198 character(*), intent(in):: file
199 character(*), intent(in), optional:: mode
200 logical, intent(out), optional:: err
201
202
203
204
205 integer, parameter:: max_unit = 99
206
207
208
209
210
211
212
213
214
215 integer, parameter:: min_unit = 0
216
217
218
219
220
221
222
223
224
225 character(TOKEN):: open_mode
226 integer:: unit_work
227 logical:: unit_exist_flag, unit_opend_flag
228 logical:: file_exist_flag
229 integer:: iostat
230 integer:: stat
231 character(STRING):: cause_c
232 character(*), parameter:: subname = 'FileOpen'
233 continue
236 cause_c = ''
237 unit = -1
238
239
240
241
242
244 open_mode = mode
245 else
246 open_mode = 'r'
247 end if
249
250
251
252
253
254 if ( trim(file) == '' ) then
256 goto 999
257 end if
258
259
260
261
262
263 unit_work = max_unit
264 do
265 inquire(unit=unit_work, exist=unit_exist_flag, opened=unit_opend_flag)
266 if (unit_exist_flag .and. .not. unit_opend_flag) then
267 exit
268 endif
269 unit_work = unit_work - 1
270 if (unit_work < min_unit) then
273 goto 999
274 end if
275 enddo
276
277
278
279
280
281 select case( trim(open_mode) )
282 case ('r', 'w', 'rw', 'a', 'ra')
283 case default
284 cause_c = open_mode
286 goto 999
287 end select
288
289
290
291
292
293 select case( trim(open_mode) )
294 case ('r')
295 inquire(file=file, exist=file_exist_flag)
296 if (.not. file_exist_flag) then
297 cause_c = file
299 goto 999
300 end if
301 end select
302
303
304
305
306
307 select case( trim(open_mode) )
308 case ('r')
309 open(unit=unit_work, iostat=iostat, &
310 & file=file, status='OLD', action='READ')
311 if (.not. iostat == 0) then
312 cause_c = file
314 goto 999
315 end if
316 close(unit=unit_work)
317 end select
318
319
320
321
322
323 select case( trim(open_mode) )
324 case ('w', 'a', 'rw', 'ra')
325 open(unit=unit_work, iostat=iostat, &
326 & file=file, status='UNKNOWN', action='WRITE')
327 if (.not. iostat == 0) then
328 cause_c = file
330 goto 999
331 end if
332 close(unit=unit_work)
333 end select
334
335
336
337
338
339 select case( trim(open_mode) )
340 case ('r')
341 open(unit=unit_work, file=file, &
342 & status='OLD', action='READ')
343
344 case ('w')
345 open(unit=unit_work, file=file, &
346 & status='REPLACE', action='WRITE')
347
348 case ('rw')
349 open(unit=unit_work, file=file, &
350 & status='REPLACE', action='READWRITE')
351
352 case ('a')
353 open(unit=unit_work, file=file, &
354 & status='UNKNOWN', position='APPEND', action='WRITE')
355
356 case ('ra')
357 open(unit=unit_work, file=file, &
358 & status='UNKNOWN', position='APPEND', action='READWRITE')
359
360 end select
361
362 unit = unit_work
363
364999 continue
subroutine, public storeerror(number, where, err, cause_c, cause_i)
integer, parameter, public dc_ebadfileopmode
integer, parameter, public dc_noerr
Error storage variables
integer, parameter, public dc_enofileread
integer, parameter, public dc_efilenameempty
integer, parameter, public dc_enofilewrite
integer, parameter, public dc_enofileexist
integer, parameter, public dc_enounitnum
Judge optional control parameters.
logical function, public present_and_not_empty(arg)
Handling character types.
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
Provides kind type parameter values.
integer, parameter, public token
Character length for word, token
integer, parameter, public string
Character length for string