将整型数组转换为字符数组在Fortran 90中会产生文件结束符(EOF)
我有一个模块,它可以接收任意数量的月份(以整数表示)。我想把这些整数转换为字符。 在一个不同的程序中,我同时使用整数和字符,因此需要进行这样的转换。
在把整数转换成字符的子程序中,在第28行 write(output_chars(idx),'(I2.2)') input_ints(idx) 时,我在循环的第一次迭代后就遇到了“end-of-file”错误。
当我在不使用 "unit=" 语句时,我没有错误。
但当我尝试使用 write(unit=output_chars(idx),'(I2.2)') input_ints(idx) 时,我得到了一个语法错误。
对整数的处理放在一个模块中(与主程序在不同的文件中)。为了完整性,我在下方的代码中包含了该模块。
我到底做错了什么?
program ConvMonInt2MonCh
use months_mod, only: get_months
implicit none
integer, allocatable :: month_int(:)
character(len=2), allocatable :: monchar(:)
call get_months(month_int)
! Call the conversion subroutine
call convert_monthint_to_monthchar(month_int, monchar)
contains
subroutine convert_monthint_to_monthchar(input_ints, output_chars)
implicit none
!Dummy arguments
integer, dimension(:), intent(in) :: input_ints
character(len=*), dimension(:), intent(out) :: output_chars
!Local loop counter
integer :: idx
!Loop through each element of the array
do idx = 1, size(input_ints)
!Write the integer into the corresponding character array element.
!'(I0)' automatically scales the character width to fit the integer exactly.
write(output_chars(idx), '(I0)') input_ints(idx)
enddo
return
end subroutine convert_monthint_to_monthchar
end program ConvMonInt2MonCh
!module: months_mod.f90
module months_mod
implicit none
contains
subroutine get_months(month_array)
implicit none
integer, intent(OUT), allocatable :: month_array(:)
integer, allocatable :: temp_array(:)
!integer, intent(out) :: int_array(:) ! Assumed-shape dummy argument
integer :: n, i, xtra_month
!1. Prompt for the number of months
!************************************
print*, 'Enter the number of months to process,'
print*, '1 for one month, 2 for two months, ...:'
print*, ' '
read *, n
!2. Allocate the array with the user-specified size
!**************************************************
ALLOCATE(month_array(n))
!3. Prompt the user for each integer
!***********************************
print*, 'Enter a month to process'
print*, "1 for Jan, or more than one month, one by one, each followed by Enter:"
print*,' '
do i = 1, n
read*, month_array(i)
enddo
!Allocate the temporary array with one extra slot
allocate(temp_array(n + 1))
!define the extra element
if(month_array(n) == 12) then
xtra_month = 1
else
xtra_month = month_array(n)+1
endif
!Copy old data into the temporary array
temp_array(1:n) = month_array(1:n)
!Add the new element to the end
temp_array(n + 1) = xtra_month
!Free the original array memory
deallocate(month_array)
!Reallocate original array with the new size
allocate(month_array(n+1))
!Copy everything back and clean up temp
month_array = temp_array
deallocate(temp_array)
end subroutine get_months
end module months_mod
解决方案
首先,对所有可能为我的问题花时间的人致以诚挚的歉意,我相信这对Fortran专家来说是一个非常基础的问题。
我意识到2output_chars2被声明为一个可分配(allocatable)的数组,但它没有被分配任何维度,因此在我试图向它写入时它是空的。
因此我通过先为它分配维度来解决,这样做:
- 不起作用,产生语法错误
allocate(output_chars(size(input_ints))
2.可行
m=size(input_ints)
allocate(output_chars(m))
谢谢大家,问题已经解决。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。