读取输入文本文件

时间:2014-03-25 11:13:56

标签: fortran90

我发现难以阅读文本文件尽快在代码中为各个变量赋值。请帮帮我,谢谢

Program Console2
IMPLICIT NONE
Real :: BB,CC,DD
Integer n,AA
Character (LEN=8) Line
OPEN(UNIT=2,FILE="Chaq.txt",STATUS='unknown')
Do  n=1,10
READ (2,('(a6,I12,F8.2,F8.2,F8.2)'))Line,AA,BB,CC,DD
write (*,*) AA,BB,CC,DD
End Do
End  program Console2

输入文件

 GRID        3954        -182.53196.65596-28.5831                           
 GRID        3955        -186.40268.61172-28.5831                           
 GRID        3957        -187.65493.24453-28.5831                           
 GRID        3958        -173.982108.158 -28.5831                           
 GRID        3959        -166.58396.77976-28.5831                           
 GRID        3960        -164.51897.80294-28.5831                           
 GRID        3961        -194.53573.86154-28.5831                           
 GRID        3962        -186.237101.5011-28.5831                           
 GRID        3963        -174.59296.25317-28.5831                           
 GRID        3965        -184.89788.38072-28.5831                           
 GRID        3966        -193.65277.39146-28.5831                           
 GRID        3967        -168.41599.97072-28.5831                           
 GRID        3968        -165.85866.80172-28.5831                           
 GRID        3970        -174.87381.45816-28.5831                           
 GRID        3971        -184.27268.54756-28.5831                           
 GRID        3972        -173.699100.7843-28.5831                           
 GRID        3973        -172.326102.0733-28.5831                           
 GRID        3974        -178.06985.87581-28.5831                           
 GRID        3975        -173.97666.80172-28.5831

1 个答案:

答案 0 :(得分:2)

您只需要考虑输入文件中的空格。尝试修改read声明:

 READ (2,('(1x,a4,8x,I4,8x,f8.2,f8.2,f8.2)')) Line,AA,BB,CC,DD

注意使用x控件编辑描述符,它告诉运行时要忽略多少个字符位置。另请注意,我已经理解您的输入文件行以一个空格开头。