读取/写入文件,然后在Fortran中创建.exe文件

时间:2011-10-24 09:01:44

标签: fortran

我是Fortran的新手,但已经获得了这些文件(见下文),它们读取输入文本文件,使用Fortran代码计算一组值,然后将它们写入输出文本文件。该程序适用于除案例4,8和11之外的所有情况,产生错误:

forrtl: severe (24): end-of-file during read, unit 5, file C:\temp\sharclab\i_capa.txt

Image              PC        Routine            Line        Source             
Capacity.exe       0046B6EE  Unknown               Unknown  Unknown
Capacity.exe       00468940  Unknown               Unknown  Unknown
Capacity.exe       00411C9A  Unknown               Unknown  Unknown
Capacity.exe       004118C7  Unknown               Unknown  Unknown
Capacity.exe       0040C416  Unknown               Unknown  Unknown
Capacity.exe       0040BBDA  Unknown               Unknown  Unknown
Capacity.exe       0040714C  _MAIN__                    16  main.F90
Capacity.exe       0046EB78  Unknown               Unknown  Unknown
Capacity.exe       004528E0  Unknown               Unknown  Unknown
kernel32.dll       7C817067  Unknown               Unknown  Unknown

我将在下面发布代码,如果有人可以帮我解决这个问题,我将非常感谢,谢谢!

第一个文件(只有不起作用的特定部分):

Case (4)

    DiamRoul = abs(DonneesRlt(1))
    DiamPrim = abs(DonneesRlt(2))
    NbRoul = abs(int(DonneesRlt(3)))
    NbRangRoul = abs(int(DonneesRlt(4)))
    LongEff = abs(DonneesRlt(5))

    If ((NbRoul==0).OR.(DiamPrim==0)) Then
        CodeErr = 1
    else ! Calcul si nb corps roulants différent de zéro

        Gamma = DiamRoul / DiamPrim

        FC = 17.24553 * ((1 + (1.04 * ((1 - Gamma) / (1 + Gamma)) 
 &      ** P1) ** P3) ** P4) * (((Gamma ** P2) * (1 - Gamma)  
 &      ** P5) / ((1 + Gamma) ** P11))

        ChargeDynSNR = FC * ((NbRangRoul * LongEff) ** P6) 
 &      * (NbRoul ** P7) * (DiamRoul ** P5) * 1.1293

        ChargeDynISO = FC * ((NbRangRoul * LongEff) ** P6)  
 &      * (NbRoul ** P7) * (DiamRoul ** P5) * 1.1

        ChargeStaSNR = 4.4 * (1 - Gamma) * NbRangRoul * NbRoul 
 &      * LongEff * DiamRoul

        ChargeStaISO = ChargeStaSNR   

    End If


Case (8)

    DiamRoul = abs(DonneesRlt(1))
    DiamPrim = abs(DonneesRlt(2))
    NbRoul = abs(int(DonneesRlt(3)))
    LongEff = abs(DonneesRlt(4))

    If ((NbRoul==0).OR.(DiamPrim==0)) Then
        CodeErr = 1
    else  ! Calcul si nb corps roulants différent de zéro       

        Gamma = DiamRoul / DiamPrim

        FC = 34.48913 * 0.85 * (Gamma ** P2)

        ChargeDynSNR = FC * (LongEff ** P6) * (NbRoul ** P7) * 
 &                      (DiamRoul ** P5) * 1

        ChargeDynISO = ChargeDynSNR

        ChargeStaSNR = 22 * (1 - Gamma) * NbRoul * LongEff 
 &                      * DiamRoul

        ChargeStaISO = ChargeStaSNR
    End If

Case (11)

    DiamRoul = abs(DonneesRlt(1))
    NbRoul = abs(int(DonneesRlt(2)))
    LongEff = abs(DonneesRlt(3))
    DiamPrim = abs(DonneesRlt(4))
    AngleDeg = 45
    AngleMin = 0
    AngleSec = 0
    NbRoul = abs(int(NbRoul / 2))
    NbRangRoul = 2
    DiamSpher = 0

    If (NbRoul==0) Then
        CodeErr = 1
    else ! Calcul si nb corps roulants différent de zéro

        Alpha = pi * (AngleDeg + (AngleMin / 60) + (AngleSec / 
 &              3600)) / 180

        If (DiamPrim==0) Then
            DiamPrim = (DiamSpher - DiamRoul) * DCos(Alpha)
        End If

        Gamma = (DiamRoul * DCos(Alpha)) / DiamPrim

        FC = 17.24553 * ((1 + (1.04 * ((1 - Gamma) / (1 + Gamma)) 
 &      ** P1) ** P3) ** P4) * (((Gamma ** P2) * (1 - Gamma)  
 &      ** P5) / ((1 + Gamma) ** P11))

        ChargeDynSNR = FC * ((NbRangRoul * LongEff * DCos(Alpha)) 
 &      ** P6) * (NbRoul ** P7) *   (DiamRoul ** P5) * 1.1

        ChargeDynISO = ChargeDynSNR

        ChargeStaSNR = 4.4 * (1 - Gamma) * NbRangRoul * NbRoul * 
 &      LongEff * DiamRoul * DCos(Alpha)

        ChargeStaISO = ChargeStaSNR
    End If

End Select  

!     Arrondissement de toutes les charges calculées
if (ChargeDynSNR.LE.0) then
    ChargeDynSNR = 0
    ChargeDynSNRArr = 0
else
    Call Arron(TypeRlt, ChargeDynSNR, CapaArr)
    ChargeDynSNRArr = CapaArr
end if

if (ChargeDynISO.LE.0) then
    ChargeDynISO = 0
    ChargeDynISOArr = 0
else
    Call Arron(TypeRlt, ChargeDynISO, CapaArr)
    ChargeDynISOArr = CapaArr
end if

if (ChargeStaSNR.LE.0) then
    ChargeStaSNR = 0
    ChargeStaSNRArr = 0
else
    Call Arron(TypeRlt, ChargeStaSNR, CapaArr)
    ChargeStaSNRArr = CapaArr
end if

if (ChargeStaISO.LE.0) then
    ChargeStaISO = 0
    ChargeStaISOArr = 0
else
    Call Arron(TypeRlt, ChargeStaISO, CapaArr)
    ChargeStaISOArr = CapaArr
endif

    !     Toutes les charges calculées sont multipliées par 10.
Charges(1) = ChargeDynSNR * 10
Charges(2) = ChargeDynISO * 10
Charges(3) = ChargeStaSNR * 10
Charges(4) = ChargeStaISO * 10
ChargesArr(1) = ChargeDynSNRArr * 10
ChargesArr(2) = ChargeDynISOArr * 10
ChargesArr(3) = ChargeStaSNRArr * 10
ChargesArr(4) = ChargeStaISOArr * 10

return
END subroutine

第二个文件(main.F90):

program main
implicit  none

integer TypeRlt
    !   Entrees/Sorties
real*8 DonneesRlt(15)
    !   Sorties 
integer CodeErr
real*8 Charges(4)
integer ChargesArr(4)

open (5, FILE='C:\temp\sharclab\i_capa.txt')
open (6, FILE='C:\temp\sharclab\o_capa.txt')

read(5,*) TypeRlt
read(5,*) DonneesRlt

write(*,*) TypeRlt
write(*,*) DonneesRlt

close(5)
!TypeRlt = 5


call Capacites(TypeRlt,DonneesRlt,Charges,ChargesArr,CodeErr)

write(6,*) ChargesArr(1)
write(6,*) ChargesArr(2)
write(6,*) ChargesArr(3)
write(6,*) ChargesArr(4)
close(6)

write(*,*) ChargesArr(1)
write(*,*) ChargesArr(2)
write(*,*) ChargesArr(3)
write(*,*) ChargesArr(4)

end

如果代码没有被正确复制,我很抱歉..两个文本文件只是一个垂直的值列表,如果没有15个值存在(此Fortran程序需要),则值为0放在它的位置。

由于此程序可以正常运行其他情况(我尚未在此处发布)。我不明白为什么它不会对这些情况做同样的事情。

2 个答案:

答案 0 :(得分:1)

只有两个读取调用,并且都尝试读取文件i_capa.txt。当您看到错误消息:“读取期间文件结束”时,您可以非常确定i_capa.txt比程序预期的要短。所以我的第一个猜测是,当程序开始执行时,i_capa.txt没有正确设置。

答案 1 :(得分:0)

尝试将单位从5和6更改为其他值:通常单位5与标准输入相关联(请参阅,例如http://docs.cray.com/books/S-3695-35/html-S-3695-35/pdollsmg.html

编辑:

i_capa.txt文件如何显示---它是否真的在两行中包含两个值?

您可以运行以下程序,看看您是否可以阅读该文件:

implicit  none

integer TypeRlt
real*8 DonneesRlt(15)

open (5, FILE='C:\temp\sharclab\i_capa.txt')

read(1,*) TypeRlt
read(1,*) DonneesRlt

print*, TypeRlt
print*, DonneesRlt

如果失败,请您发布i_capa.txt文件的确切内容。

我怀疑您要么没有该文件,要么DonneesRlt的实际值少于15,或者它包含其他内容。