Open file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(file_object), | intent(inout) | :: | self | File object. |
||
| character(len=*), | intent(in), | optional | :: | file_name | File name. |
|
| character(len=*), | intent(in), | optional | :: | format | File format. |
|
| character(len=*), | intent(in), | optional | :: | action | File action. |
|
| character(len=*), | intent(in), | optional | :: | access | File access. |
subroutine open(self, file_name, format, action, access)
!< Open file.
class(file_object), intent(inout) :: self !< File object.
character(len=*), intent(in), optional :: file_name !< File name.
character(len=*), intent(in), optional :: format !< File format.
character(len=*), intent(in), optional :: action !< File action.
character(len=*), intent(in), optional :: access !< File access.
character(len=:), allocatable :: file_name_ !< File name, local variable.
character(len=:), allocatable :: format_ !< File format, local variable.
character(len=:), allocatable :: action_ !< File action, local variable.
character(len=:), allocatable :: access_ !< File access, local variable.
format_ = 'unformatted' ; if (present(format)) format_ = format
action_ = 'readwrite' ; if (present(action)) action_ = action
access_ = 'stream' ; if (present(access)) access_ = access
if (self%is_initialized) then
file_name_ = self%file_name ; if (present(file_name)) file_name_ = trim(adjustl(file_name))
if (.not.self%is_connected) then
open(newunit=self%file_unit, file=file_name_, form=format_, action=action_, access=access_)
self%is_connected = .true.
else
write(stderr, '(A)') 'error: file "'//self%file_name//'" is already connected, thus its unit cannot be re-open'
self%error%status = ERROR_ALREADY_CONNECTED
endif
else
write(stderr, '(A)') 'error: file is not initialized, thus it cannot be open'
self%error%status = ERROR_NOT_INITIALIZED
endif
endsubroutine open