121 SUBROUTINE close_file(unit_number, file_status, keep_preconnection)
123 INTEGER,
INTENT(IN) :: unit_number
124 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_status
125 LOGICAL,
INTENT(IN),
OPTIONAL :: keep_preconnection
127 CHARACTER(LEN=2*default_path_length) :: message
128 CHARACTER(LEN=6) :: status_string
129 CHARACTER(LEN=default_path_length) :: file_name
131 LOGICAL :: exists, is_open, keep_file_connection
133 keep_file_connection = .false.
134 IF (
PRESENT(keep_preconnection)) keep_file_connection = keep_preconnection
136 INQUIRE (unit=unit_number, exist=exists, opened=is_open, iostat=istat)
139 WRITE (unit=message, fmt=
"(A,I0,A,I0,A)") &
140 "An error occurred inquiring the unit with the number ", unit_number, &
141 " (IOSTAT = ", istat,
")"
142 cpabort(trim(message))
143 ELSE IF (.NOT. exists)
THEN
144 WRITE (unit=message, fmt=
"(A,I0,A)") &
145 "The specified unit number ", unit_number, &
146 " cannot be closed, because it does not exist."
147 cpabort(trim(message))
155 WRITE (unit=message, fmt=
"(A,I0)") &
156 "Attempt to close the default input unit number ", unit_number
157 cpabort(trim(message))
160 WRITE (unit=message, fmt=
"(A,I0)") &
161 "Attempt to close the default output unit number ", unit_number
162 cpabort(trim(message))
165 IF (
PRESENT(file_status))
THEN
166 status_string = trim(file_status)
168 status_string =
"KEEP"
171 INQUIRE (unit=unit_number, name=file_name, iostat=istat)
173 WRITE (unit=message, fmt=
"(A,I0,A,I0,A)") &
174 "An error occurred inquiring the unit with the number ", unit_number, &
175 " (IOSTAT = ", istat,
")."
176 cpabort(trim(message))
179 IF (keep_file_connection)
THEN
180 CALL assign_preconnection(file_name, unit_number)
182 CALL delete_preconnection(file_name, unit_number)
183 CLOSE (unit=unit_number, iostat=istat, status=trim(status_string))
185 WRITE (unit=message, fmt=
"(A,I0,A,I0,A)") &
186 "An error occurred closing the file with the logical unit number ", &
187 unit_number,
" (IOSTAT = ", istat,
")."
188 cpabort(trim(message))
308 SUBROUTINE open_file(file_name, file_status, file_form, file_action, &
309 file_position, file_pad, unit_number, debug, &
310 skip_get_unit_number, file_access)
312 CHARACTER(LEN=*),
INTENT(IN) :: file_name
313 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_status, file_form, file_action, &
314 file_position, file_pad
315 INTEGER,
INTENT(INOUT) :: unit_number
316 INTEGER,
INTENT(IN),
OPTIONAL :: debug
317 LOGICAL,
INTENT(IN),
OPTIONAL :: skip_get_unit_number
318 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_access
320 CHARACTER(LEN=*),
PARAMETER :: routinen =
'open_file'
322 CHARACTER(LEN=11) :: access_string, action_string, current_action, current_form, &
323 form_string, pad_string, position_string, status_string
324 CHARACTER(LEN=2*default_path_length) :: message
325 CHARACTER(LEN=default_path_length) :: cwd, iomsgstr, real_file_name
326 INTEGER :: debug_unit, istat
327 LOGICAL :: exists, get_a_new_unit, is_open
329 IF (
PRESENT(file_access))
THEN
330 access_string = trim(file_access)
332 access_string =
"SEQUENTIAL"
335 IF (
PRESENT(file_status))
THEN
336 status_string = trim(file_status)
338 status_string =
"OLD"
341 IF (
PRESENT(file_form))
THEN
342 form_string = trim(file_form)
344 form_string =
"FORMATTED"
347 IF (
PRESENT(file_pad))
THEN
348 pad_string = file_pad
349 IF (form_string ==
"UNFORMATTED")
THEN
350 WRITE (unit=message, fmt=
"(A)") &
351 "The PAD specifier is not allowed for an UNFORMATTED file."
352 cpabort(trim(message))
358 IF (
PRESENT(file_action))
THEN
359 action_string = trim(file_action)
361 action_string =
"READ"
364 IF (
PRESENT(file_position))
THEN
365 position_string = trim(file_position)
367 position_string =
"REWIND"
370 IF (
PRESENT(debug))
THEN
376 IF (file_name(1:1) ==
" ")
THEN
377 WRITE (unit=message, fmt=
"(A)") &
378 "The file name <"//trim(file_name)//
"> has leading blanks."
379 cpabort(trim(message))
382 IF (status_string ==
"OLD")
THEN
386 real_file_name = trim(adjustl(file_name))
387 IF (len_trim(real_file_name) == 0)
THEN
388 cpabort(
"A file name length of zero for a new file is invalid.")
393 INQUIRE (file=trim(real_file_name), exist=exists, opened=is_open, iostat=istat)
396 WRITE (unit=message, fmt=
"(A,I0,A)") &
397 "An error occurred inquiring the file <"//trim(real_file_name)// &
398 "> (IOSTAT = ", istat,
")"
399 cpabort(trim(message))
400 ELSE IF (status_string ==
"OLD")
THEN
401 IF (.NOT. exists)
THEN
402 WRITE (unit=message, fmt=
"(A)") &
403 "The specified OLD file <"//trim(real_file_name)// &
404 "> cannot be opened. It does not exist. "// &
406 cpabort(trim(message))
412 INQUIRE (file=trim(real_file_name), number=unit_number, &
413 action=current_action, form=current_form)
414 IF (trim(position_string) ==
"REWIND") rewind(unit=unit_number)
415 IF (trim(status_string) ==
"NEW")
THEN
416 CALL cp_abort(__location__, &
417 "Attempt to re-open the existing OLD file <"// &
418 trim(real_file_name)//
"> with status attribute NEW.")
420 IF (trim(current_form) /= trim(form_string))
THEN
421 CALL cp_abort(__location__, &
422 "Attempt to re-open the existing "// &
423 trim(current_form)//
" file <"//trim(real_file_name)// &
424 "> as "//trim(form_string)//
" file.")
426 IF (trim(current_action) /= trim(action_string))
THEN
427 CALL cp_abort(__location__, &
428 "Attempt to re-open the existing file <"// &
429 trim(real_file_name)//
"> with the modified ACTION attribute "// &
430 trim(action_string)//
". The current ACTION attribute is "// &
431 trim(current_action)//
".")
435 get_a_new_unit = .true.
436 IF (
PRESENT(skip_get_unit_number))
THEN
437 IF (skip_get_unit_number) get_a_new_unit = .false.
439 IF (get_a_new_unit) unit_number =
get_unit_number(trim(real_file_name))
440 IF (unit_number < 1)
THEN
441 WRITE (unit=message, fmt=
"(A)") &
442 "Cannot open the file <"//trim(real_file_name)// &
443 ">, because no unused logical unit number could be obtained."
444 cpabort(trim(message))
446 IF (trim(form_string) ==
"FORMATTED")
THEN
447 OPEN (unit=unit_number, &
448 file=trim(real_file_name), &
449 status=trim(status_string), &
450 access=trim(access_string), &
451 form=trim(form_string), &
452 position=trim(position_string), &
453 action=trim(action_string), &
454 pad=trim(pad_string), &
458 OPEN (unit=unit_number, &
459 file=trim(real_file_name), &
460 status=trim(status_string), &
461 access=trim(access_string), &
462 form=trim(form_string), &
463 position=trim(position_string), &
464 action=trim(action_string), &
470 WRITE (unit=message, fmt=
"(A,I0,A,I0,A)") &
471 "An error occurred opening the file '"//trim(real_file_name)// &
472 "' (UNIT = ", unit_number,
", IOSTAT = ", istat,
"). "//trim(iomsgstr)//
". "// &
473 "Current working directory: "//trim(cwd)
475 cpabort(trim(message))
479 IF (debug_unit > 0)
THEN
480 INQUIRE (file=trim(real_file_name), opened=is_open, number=unit_number, &
481 position=position_string, name=message, access=access_string, &
482 form=form_string, action=action_string)
483 WRITE (unit=debug_unit, fmt=
"(T2,A)")
"BEGIN DEBUG "//trim(routinen)
484 WRITE (unit=debug_unit, fmt=
"(T3,A,I0)")
"NUMBER : ", unit_number
485 WRITE (unit=debug_unit, fmt=
"(T3,A,L1)")
"OPENED : ", is_open
486 WRITE (unit=debug_unit, fmt=
"(T3,A)")
"NAME : "//trim(message)
487 WRITE (unit=debug_unit, fmt=
"(T3,A)")
"POSITION: "//trim(position_string)
488 WRITE (unit=debug_unit, fmt=
"(T3,A)")
"ACCESS : "//trim(access_string)
489 WRITE (unit=debug_unit, fmt=
"(T3,A)")
"FORM : "//trim(form_string)
490 WRITE (unit=debug_unit, fmt=
"(T3,A)")
"ACTION : "//trim(action_string)
491 WRITE (unit=debug_unit, fmt=
"(T2,A)")
"END DEBUG "//trim(routinen)
492 CALL print_preconnection_list(debug_unit)
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.