【发布时间】:2018-03-01 13:00:32
【问题描述】:
我正在努力阅读一个文本字符串。我正在使用 gfortran 4.9.2。
下面我写了一个小子程序,我想在其中提交写格式作为参数。
理想情况下,我希望能够调用它
call printarray(mat1, "F8.3")
例如以该格式打印出矩阵 mat1。列数应在子程序内自动确定。
subroutine printarray(x, udf_temp)
implicit none
real, dimension(:,:), intent(in) :: x ! array to be printed
integer, dimension(2) :: dims ! array for shape of x
integer :: i, j
character(len=10) :: udf_temp ! user defined format, eg "F8.3, ...
character(len = :), allocatable :: udf ! trimmed udf_temp
character(len = 10) :: udf2
character(len = 10) :: txt1, txt2
integer :: ncols ! no. of columns of array
integer :: udf_temp_length
udf_temp_length = len_trim(udf_temp)
allocate(character(len=udf_temp_length) :: udf)
dims = shape(x)
ncols = dims(2)
write (txt1, '(I5)') ncols
udf2 = trim(txt1)//adjustl(udf)
txt2 = "("//trim(udf2)//")"
do i = 1, dims(1)
write (*, txt2) (x(i, j), j = 1, dims(2)) ! this is line 38
end do
end suroutine printarray
当我设置len = 10:
character(len=10) :: udf_temp
我得到编译错误:
call printarray(mat1, "F8.3")
1
Warning: Character length of actual argument shorter than of dummy argument 'udf_temp' (4/10) at (1)
当我设置len = *
character(len=*) :: udf_temp
它编译但在运行时:
At line 38 of file where2.f95 (unit = 6, file = 'stdout')
Fortran runtime error: Unexpected element '( 8
我做错了什么? 有没有更简洁的方法来做到这一点?
【问题讨论】:
-
当 len=10 时的调试输出在文件 where2.f95 (unit = 6, file = 'stdout') 的第 38 行 Fortran 运行时错误:Unexpected element '( 8 [Inferior 1 (process 19607) exited代码 02] len=* 时的调试输出 where2.f95 (unit = 6, file = 'stdout') Fortran 运行时错误:意外元素'( 8 [Inferior 1 (process 19634) exited with code 02]
-
不要把你想让人们理解的材料放入 cmets,格式不够好。
-
您实际上并没有在任何地方使用
udf_temp的 值 - 投票关闭只是一个简单的错字。如果你能解释一些真正的误解,我可能会撤回投票。 -
此外,您在引用它之前没有定义
udf(在udf2 = trim(txt1)//adjustl(udf)中).. -
令人费解的是,
"8"是如何出现在错误消息中的。我认为显示的代码并不是真正给出该错误的原因。