这是我拥有的工作代码。当前,它读取事件文件(file=1)的第一行,然后读取工作站文件(file=2)的第一行,并将它们写出,执行go to命令,返回并读取工作站文件中的下一行,然后再次将其全部写出,并遍历该文件,直到读完工作站文件的最后一行。
实际上,我需要的是再次“循环”整个事件,以便现在可以读取和写出事件文件(file=1)的第二行,然后对事件文件中的所有行执行此操作。
我尝试使用另一个"go to“命令,但我的输出从来没有改变过当前代码输出的内容。
有没有人知道如何添加另一个go to command,让它再次循环通过这个东西?
program events
implicit none
character*40 aline
character*40 bline
open (1, file="event", status="old")
open (2, file="stations", status="old")
open (3, file="output", status="new")
read(1, '(a40)',end=60) aline
1 read(2, '(a40)',end=60) bline
write(3,*) aline, bline
go to 1
60 stop
end program events
发布于 2021-10-13 03:15:28
我会完全避免使用goto
-在现代Fortran中很少有任何需要,虽然我并不是狂热分子,但它们通常出现在更难阅读的代码中。这就是我要做的--基本上,当你有两个循环时,我会写两个循环:
ijb@ijb-Latitude-5410:~/work/stack$ cat british_rail.f90
Program events
Use, Intrinsic :: iso_fortran_env, Only : eof => iostat_end
Implicit None
Integer :: io_status
Character( Len = 40 ) :: aline
Character( Len = 40 ) :: bline
Open( 10, file="event" , status="old" )
Open( 20, file="stations", status="old" )
Open( 30, file="output" , status="new" )
events_read: Do
Read( 10, '(a40)', iostat = io_status) aline
If( io_status == eof ) Exit events_read
stations_read: Do
Read( 20, '(a40)', iostat = io_status ) bline
If( io_status == eof ) Exit stations_read
Write( 30, * ) aline, bline
End Do stations_read
Rewind 20
End Do events_read
End Program events
ijb@ijb-Latitude-5410:~/work/stack$ gfortran --version
GNU Fortran (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ijb@ijb-Latitude-5410:~/work/stack$ gfortran -Wall -Wextra -std=f2008 -fcheck=all -g british_rail.f90
ijb@ijb-Latitude-5410:~/work/stack$ cat event
Leaves on track
Car Stuck on Level Crossing
Wrong kind of snow
ijb@ijb-Latitude-5410:~/work/stack$ cat stations
Leicester
Market Harborough
Bedford
St Pancras
ijb@ijb-Latitude-5410:~/work/stack$ cat output
cat: output: No such file or directory
ijb@ijb-Latitude-5410:~/work/stack$ ./a.out
ijb@ijb-Latitude-5410:~/work/stack$ cat output
Leaves on track Leicester
Leaves on track Market Harborough
Leaves on track Bedford
Leaves on track St Pancras
Car Stuck on Level Crossing Leicester
Car Stuck on Level Crossing Market Harborough
Car Stuck on Level Crossing Bedford
Car Stuck on Level Crossing St Pancras
Wrong kind of snow Leicester
Wrong kind of snow Market Harborough
Wrong kind of snow Bedford
Wrong kind of snow St Pancras
ijb@ijb-Latitude-5410:~/work/stack$
发布于 2021-10-12 23:20:11
将read from 2上的end=更改为read from 1。您需要倒回2,因为您已到达结尾。
50 read(1, '(a40)',end=60) aline
rewind 2
1 read(2, '(a40)',end=50) bline
write(3,*) aline, bline
go to 1
60 stop
https://stackoverflow.com/questions/69548628
复制相似问题