Fortran 不写入输出文件或打印到屏幕

问题描述 投票:0回答:1

我编写了这个简单的 Fortran 代码,目的是将答案写入输出文件。相反,输出文件是空白的,即使当我尝试打印到屏幕上时,它也不会这样做。

这是代码,很短:

    program FTCS
    implicit none
    integer :: m,n,i,j,a=1
    real, allocatable :: P(:,:)
    real :: r,l,t,delta_x,delta_t,g

    open(20,file='input.txt')
    open(30,file='outputFTCS.txt')
    read(20,2)n  ! reading these values from the input.txt file
    read(20,3)r,t,delta_t,l  ! reading these values from input.txt
    
    2 format (T10,I6)  ! I is for integers
    3 format (T10,F15.8)  ! F is for real (float)
    
    delta_x = 1/(n-1)  ! n = points
    g = floor(t/delta_t)
    m = g  ! time
    
    allocate (P(m,n))
    P(1,:)=20  ! initial condition: indexing begins at 1
    
    do i=2,m    ! points
        P(i,1)=100 ! boundary condition (left)
        P(i,n)=0  ! boundary condition (right)
        do j=2,n-1  ! time
            P(i,j)=r*(P(i-1,j+1)+P(i-1,j-1))+(1-2*r)*(P(i-1,j))  ! T_i=r(T_i-1 + T_i+1) + (1-2r)T_i
            print *,"hello"
        end do
    end do
    
    do i=1,m
        print *,'hello'
        
    end do
    
    10 format(10f10.2)
    30 format (10f10.2)
    
    close(10)  ! do this last
    close(30)

    end program FTCS

我使用

gfortran FTCS.f90
进行编译,然后尝试使用以下命令运行:
./FTCS
./a.out
,但都没有产生输出。我也用
gfortran FTCS.f90 -o
编译来达到相同的结果。

input.txt 文件就是

    n=10     
    r=0.25     
    t=.03     
    delta_t=0.005     
    l=1

为什么它不写入输出文件,甚至不向屏幕写入任何内容?

fortran gfortran
1个回答
0
投票

我认为这里有几个问题。首先,您的输入文件不仅包含数字,还包含字符。如果您将输入文件更改为如下所示:

10
0.25
.03
0.005
1

你的代码看起来像:

    program FTCS
    implicit none
    integer :: m,n,i,j,a=1
    real, allocatable :: P(:,:)
    real :: r,l,t,delta_x,delta_t,g

    open(20,file='input.txt')
    open(30,file='outputFTCS.txt')
    read(20,*)n  ! reading these values from the input.txt file
    read(20, *) r, t, delta_t, l

write(*,*) n, r, t, delta_t, l


end program

您会注意到它现在可以工作了:

:~/work/random$ ./a.out
          10  0.250000000       2.99999993E-02   4.99999989E-03   1.00000000

如果您想保留

n=
,您必须将该行作为字符读取,然后进行一些拆分以准确提取您需要的内容。

© www.soinside.com 2019 - 2024. All rights reserved.