我正在使用 fortran 代码,我的输入文件如下所示:
...
Binning n: 4, "Si2-Events", Event #: 12, Primary(s) weight 1.0000E+00
Number of hit cells: 3
488534 4.23038400E-05 489533 1.50734719E-04 489534 5.79968946E-05
Binning n: 4, "Si2-Events", Event #: 13, Primary(s) weight 1.0000E+00
Number of hit cells: 2
477500 3.04398331E-04 478500 1.13192732E-06
Binning n: 4, "Si2-Events", Event #: 14, Primary(s) weight 1.0000E+00
Number of hit cells: 2
512496 1.32522946E-05 513496 2.86743394E-04
Binning n: 4, "Si2-Events", Event #: 15, Primary(s) weight 1.0000E+00
Number of hit cells: 2
476539 1.95245666E-04 476540 2.37216373E-05
Binning n: 4, "Si2-Events", Event #: 16, Primary(s) weight 1.0000E+00
Number of hit cells: 9
502533 1.26090490E-05 502534 1.00212252E-04 503527 3.07000097E-04 503528 9.53662311E-06 503529 9.42530642E-06 503530 1.07992764E-05 503531 1.26466557E-05 503532 1.68176994E-05 503533 1.18242851E-05
...
换句话说,我有一个包含很多行的文件,每行在第三行显示一个单元格编号和能量,例如
488534 4.23038400E-05 489533 1.50734719E-04 489534 5.79968946E-05
我想编写一个只读取这一行的fortran代码,并将两列中的单元格编号和能量写入输出文件,例如
Line 1
Cells 488534
489533
489534
Energy
4.23038400E-05
1.50734719E-04
5.79968946E-05
Line 2
Cells 477500
478500
Energy 3.04398331E-04
1.13192732E-06
etc...
问题是单元格的数量因行而异。读取所有值后,如何使其跳到下一行?
这是我测试的一些代码:
open (unit=7, file="Si1.txt", action="read", access="sequential")
open (unit=8, file="output.txt", action="write")
do i = 1, 900
read (7,*) line1
read (7,*) line2
read (7,*) cell1, energy1, cell2, energy2
write(8,*) "Run = ", i, "and cells = ", cell1, cell2, "and energy = ", energy1, energy2
end do
问题在于,这仅在该行中有两个或多个值时才有效,而如果它小于两个则无效。
我有点迷路(可能是这里的菜鸟),但是关于如何使这项工作的任何建议?