我正在使用子程序输出从前一个函数收集的数组。在运行我的程序并且只调用子程序一次后,我的目标文件名testf.txt被命名为“testf.txtÑ@”。认为这是我从朋友那里借来的一个隐藏的角色,我重新整理了整个部分但没有改变。当我进行多次调用时,文件的名称会相互添加。意图是“testf.txt”“testg.txt”“testh.txt”,给定,“testf.txttestg”“testg.txttesth”和“testh.txt”。内容很好,名称是唯一的问题。这是由代码中隐藏的字符引起的,还是子程序如何保存信息的逻辑错误?
谢谢,
托马斯
主程序
program Main
implicit none
!DATA DICTIONARY
character :: selection ! Method of summation
! L = left Riemann sum
! R = right Riemann sum
! M = Midpoint rule
! T = Trapezoidal rule sum
! S = Simpson's rule
integer :: n ! Number of subintervals the integral is to
! be divided into
integer :: func ! The function to be integrated
! f = (28.0/5) + 2*sin(4*x) - (1/x)
! g = ln(4x) + (19x)^3 - 43
! h = 1/(sqrt(2.0*PI))*exp(-(x**2)/2.0)
character(20) :: filename
real, external :: f
real, external :: g
real, external :: h
real, dimension(1000) :: area
!Declare functions used
real :: Quad
write(*,*) "Written in the form ",
+ "Quad(func, n, left, right, selection):"
write(*,*) " "
!Test Drivers
write(*, *) Quad(f, 10, 0.2, 3.2, 'T', area)
call Out(10, area, 'testf.txt')
write(*, *) Quad(g, 10, 5.0, 10.0, 'L', area)
call Out(10, area, 'testg.txt')
write(*, *) Quad(h, 25, 1.0, 10.0, 'L', area)
call Out(10, area, 'testh.txt')
end program Main
实际输出子程序
subroutine Out(n, area, filename)
!PRE: n > 0
! area is the initialized array area(1...n)
! filename is initialized
implicit none
integer, intent(in) :: n
real, dimension(n), intent(in) :: area
character(20), intent(in) :: filename
integer :: i
open(unit=1, file=filename, form='formatted', !Writes area(1...n) to a file
+ action='write', status='replace')
do i = 1, n, 1
write(1,*) area(i)
end do
close(unit=1)
end subroutine Out
答案 0 :(得分:1)
这种错误通常表示以下一个或多个:
您通常可以通过以下方式解决此类问题:a)打开所有编译器错误检查并修复所有警告和/或b通过类似{1}}等lint运行代码并修复发现的问题