我正在尝试将假定的排名数组写入文件:
subroutine write_dbl_gen(filename, array)
Implicit None
character(len=*), intent(in) :: filename
real(8), intent(in) :: array(..)
open(unit=p_un, file=filename, form="unformatted",&
access="stream")
write (p_un) array
close(unit=p_un)
end subroutine write_dbl_gen
ifort 然后提示:
(base) > $ make [±master ●]
[ 25%] Building Fortran object CMakeFiles/npy.x.dir/src/npy.F90.o
/Users/redies/NPY-for-Fortran/src/npy.F90(835): error #8842: An I/O list item must not be an assumed rank object. [ARRAY]
write (p_un) array
---------------------^
compilation aborted for /Users/redies/NPY-for-Fortran/src/npy.F90 (code 1)
make[2]: *** [CMakeFiles/npy.x.dir/src/npy.F90.o] Error 1
make[1]: *** [CMakeFiles/npy.x.dir/all] Error 2
make: *** [all] Error 2
如何将假定的等级数组写入文件?我尝试 reshape 它,但“ reshape ”功能不会采用假定的等级数组。我使用“转移”尝试了类似的事情,但它有同样的问题。
最佳答案
假定级别的实体出现的位置非常有限。特定约束(Fortran 2018,C837)说:
An assumed-rank variable name shall not appear in a designator or expression except as an actual argument that corresponds to a dummy argument that is assumed-rank, the argument of the function C_LOC or C_SIZEOF from the intrinsic module ISO_C_BINDING (18.2), the first dummy argument of an intrinsic inquiry function, or the selector of a SELECT RANK statement.
ifort 提示在问题的子程序中使用是正确的。
正如 Vladimir F 评论的那样,可以使用 SELECT RANK 构造来创建一个非假定等级实体,然后可以将其用于(可能)多个写入语句(每个等级一个)。
但是,如 an answer elsewhere 中所述,可以将一些假设级别的实体“转换”为另一个已知级别的实体,然后可以在写入语句中使用:
real(8), pointer :: output_array(:)
call c_f_pointer(c_loc(array), output_array, [size(array)])
write (p_un) output_array
因为使用了c_loc
来自 iso_c_binding
这里array
应该有 target
添加了属性。
在我上面的例子中,size
是一个内在的查询功能,这就是为什么 size(array)
被允许。从问题中,reshape
和 transfer
而是转换函数,解释了为什么它们不接受假定秩数组作为参数。
关于arrays - 将假定等级实体写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938045/