数据入口错误Fortran

时间:2016-09-04 17:07:44

标签: fortran

我正在学习如何使用fortran90编程,我需要通过命令提示符从txt文件接收数据(类似的内容:
 的Program.exe"<" data.txt中)。 在输入txt文件中,我总是有一行至少有6个数字,直到无穷大。

如果数据是逐行写入的,它运行正常,但作为单行我收到错误:" traceback:不可用,编译时使用-ftrace = frame或-ftrace = full fortran运行时错误:结束文件"

*注意:我正在使用Force fortran 2.0

这是数据的例子:

0 1 0.001 5 3 1 0 -9 3

编辑:澄清一下:除了read语句之外,代码工作正常,这是一个简单的" read *,"。 我想知道如何通过带有流方向的promt命令 来读取txt中的整行。 (你可以在这里看到更多相关内容:https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true)。 没有必要阅读代码,我发布它只是为了知识。

对于给您带来的不便,我感到很抱歉。

这是迄今为止的代码:

program bissecao
implicit none
integer::cont,int,e,k,intc,t1,t2,t3
doubleprecision::ii,is,pre,prec,erro,somaa,somab,xn
doubleprecision,dimension(:),allocatable::co
t1=0
t2=0
t3=0


                                                                                       ! print*,"insira um limite inf da funcao"
read*,ii
                                                                                        !print*,"insira o limite superior da func"
read*,is
                                                                                       ! print*,"insira a precisÆo admissivel"
read*,pre
                                                                                      if (erro<=0) then !elimina criterio de parada negativo ou zero
                                                                                          Print*,"erro"
                                                                                          go to 100
                                                                                          end if
                                                                                        !print*,"insira a qtd  iteracoes admissiveis"
read*,int
                                                                                        !print*,"insira o grau da f(x)"
read*,e
                                                                                      if (e<=0) then  ! elimina expoente negativo
                                                                                          e=(e**2)**(0.5)
                                                                                          end if
allocate(co(e+1))
                                                                                        !print*, "insira os coeficientes na ordem:&
                                                                                               ! &c1x^n+...+(cn-1)x^1+cnx^0"
read(*,*)(co(k),k=e+1,1,-1)


somab=2*pre
intc=0


        do while (intc<int.and.(somab**2)**0.5>pre.and.((is-ii)**2)**0.5>pre)

           somab=0
           somaa=0
                 xn =(ii+is)/2

                     do k=1,e+1,1
                        if (ii /=0) then
                           somaa=ii**(k-1)*co(k)+somaa
                        else
                        somaa=co(1)
                        end if
       ! print*,"somaa",k,"=",somaa
                     end do
                     do k=1,(e+1),1
                       if (xn/=0) then
                       somab=xn**(k-1)*co(k)+somab
                       else
                       somab=co(1)
                       end if
        !print*,"somab",k,"=",somab
                     end do

                if ((somaa*somab)<0) then
                       is=xn
                else if((somaa*somab)>0)then
                       ii=xn
                else if ((somaa*somab)==0) then
                       xn=(ii+is)/2
                       go to 100
                end if
                intc =intc+1
                prec=is-ii
                if ((((is-ii)**2)**.5)< pre) then
                t3=1
                end if
                if (((somab**2)**.5)< pre) then
                t2=1.
                end if
                if (intc>=int) then
                t1=1
                end if
        end do
        somab=0
                      xn=(ii+is)/2
                    do k=1,(e+1),1
                       if (xn/=0) then
                       somab=xn**(k-1)*co(k)+somab
                       else
                       somab=co(1)
                       end if
                     end do


100 write(*,'(A,F20.15,A,F20.15,A,A,F20.15,A,F20.15,A,I2)'),"I:[",ii,",",is,"]","raiz:",xn,"Fraiz:",somab,"Iteracoes:",intc

end program   !----------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

在你的程序中,你正在使用&#34; list-directed输入&#34; (即read *,read(*,*)

read *, ii
read *, is
read *, pre
read *, int
read *, e
read *, ( co( k ), k = e+1, 1, -1 )

表示程序在每个read语句之后转到数据文件的下一行(忽略同一行中的任何剩余数据)。因此,如果数据文件(例如&#34; multi.dat&#34;)由单独的行组成(如OP建议的那样),程序就可以工作:

0
1
0.001
5
3
1 0 -9 3

但是现在你正在尝试读取只包含一行的输入文件(例如&#34; single.dat&#34;)

0 1 0.001 5 3 1 0 -9 3

在这种情况下,我们需要使用单个read语句读取所有值(如果要使用列表定向输入)。

这里的一个细微之处是数组co的范围取决于e,它也需要由相同的read语句读取。解决方法可能是预先分配co具有足够多的元素(比如100)并在一行中读取数据,例如,

integer :: k
allocate( co( 100 ) )
read *, ii, is, pre, int, e, ( co( k ), k = e+1, 1, -1 )

为了完整性,这是一个测试程序,您可以选择method = 1或2来阅读&#34; multi.dat&#34;或&#34; single.dat&#34;。

program main
    implicit none
    integer :: int, e, k, method
    double precision :: ii, is, pre
    double precision, allocatable :: co(:)

    allocate( co( 1000 ) )

    method = 1   !! 1:multi-line-data, 2:single-line-data

    if ( method == 1 ) then
        call system( "cat multi.dat" )
        read*, ii
        read*, is
        read*, pre
        read*, int
        read*, e
        read*, ( co( k ), k = e+1, 1, -1 )
    else
        call system( "cat single.dat" )
        read*, ii, is, pre, int, e, ( co( k ), k = e+1, 1, -1 )
    endif

    print *, "Input data obtained:"
    print *, "ii  = ", ii
    print *, "is  = ", is
    print *, "pre = ", pre
    print *, "int = ", int
    print *, "e   = ", e

    do k = 1, e+1
        print *, "co(", k, ") = ", co( k )
    enddo
end program

您可以将标准输入的输入文件作为

传递
./a.out < multi.dat   (for method=1)
./a.out < single.dat  (for method=2)

请注意&#34; multi.dat&#34;也可以使用&#34;&lt;&#34;。

直接阅读