[英]Reading bad values from binary file in Fortran with a defined input procedure
我正在嘗試編寫一個簡單的代碼,該代碼將具有相同父級抽象類的某些對象帶入一個二進制文件中,然后將它們讀回。
我的代碼如下所示:
module m
implicit none
type :: container
class(a), allocatable :: item
end type container
type, abstract :: a
character(20), public :: obj_type
integer, public :: num
contains
procedure :: write_impl => write_a
procedure :: read_impl => read_a
generic :: write(unformatted) => write_impl
generic :: read(unformatted) => read_impl
end type a
type, extends(a) :: b
integer, public :: num2
contains
procedure :: write_impl => write_b
procedure :: read_impl => read_b
end type b
type, extends(a) :: c
end type c
contains
subroutine write_a(this, unit, iostat, iomsg)
class(a), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
write(unit, iostat=iostat, iomsg=iomsg) this%num
end subroutine write_a
subroutine read_a(this, unit, iostat, iomsg)
class(a), intent(inout) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
read(unit, iostat=iostat, iomsg=iomsg) this%num
end subroutine read_a
subroutine write_b(this, unit, iostat, iomsg)
class(b), intent(in) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
write(unit, iostat=iostat, iomsg=iomsg) this%num, this%num2
end subroutine write_b
subroutine read_b(this, unit, iostat, iomsg)
class(b), intent(inout) :: this
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
read(unit, iostat=iostat, iomsg=iomsg) this%num, this%num2
end subroutine read_b
end module m
program mwe
use m
implicit none
class(a), allocatable :: o1, o2, o3, o4
class(container), allocatable :: arr(:)
integer :: i, arr_size, tmp
character(20) :: str_tmp
o1 = b('b', 1, 2)
o2 = c('c', 3)
allocate(arr(2))
arr(1)%item = o1
arr(2)%item = o2
select type(t => o1)
type is(b)
write(*,*) t%num, t%num2
end select
select type(t => arr(1)%item)
type is(b)
write(*,*) t%num, t%num2
end select
write(*,*) 'Write into binary'
! WRITE size
open(123, file='test5.dat', form='unformatted')
write(123) SIZE(arr)
do i=1,2
write(123) arr(i)%item%obj_type
if(arr(i)%item%obj_type .eq. 'b') then
select type(t => arr(i)%item)
type is(b)
write(123) t
end select
else if(arr(i)%item%obj_type .eq. 'c') then
select type(t => arr(i)%item)
type is(c)
write(123) t
end select
end if
end do
close(123)
write(*,*) 'Read from binary'
open(123, file='test5.dat', form='unformatted')
read(123) arr_size
write(*,*) 'array size: ', arr_size
do i=1,2
read(123) str_tmp
write(*,*) str_tmp
if(allocated(o3)) deallocate(o3)
if(str_tmp .eq. 'b') then
allocate(b :: o3)
select type(t => o3)
type is(b)
read(123) t
write(*,*) t%num, t%num2 ! BAD OUTPUT
end select
else if(str_tmp .eq. 'c') then
allocate(c :: o3)
select type(t => o3)
type is(c)
read(123) t
write(*,*) t%num
end select
end if
end do
end program mwe
問題是,在讀取o1
,它的類型為b
,即該對象具有兩個組件num
和num2
。 我存儲了它,我自然希望讀取的值與寫入的值相同。
但是我得到了與在Fortran數組中刪除變量中描述的相同的奇怪行為嗎? 。 在這個問題中,這是由初始化數組時分配語法錯誤引起的,但是在這種情況下,我完全不知道為什么我的輸出看起來像這樣:
1 2
1 2
Write into binary
Read from binary
array size: 2
b
1 0
c
3
b
下的值顯然應該是1和2,而不是1和0。我在做什么錯?
這里的問題是
select type(t => arr(i)%item)
type is(b)
write(123) t
end select
ifort實際上沒有選擇過程write_b
來處理定義的輸出。 在處理定義的輸入時,它也不選擇過程read_b
。
而是選擇過程write_a
和read_a
。
這是編譯器的問題,應報告給英特爾。 一個相當繁瑣的解決方法是在那些過程中select type
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.