How to nest another Go To command when I am already using one

Viewed 60

This is the working code I have. Currently it reads in the first line of my events file (file=1) and then the first line of my stations file (file=2) and writes them out, performs a go to command and goes back and reads the next line in the stations file and then writes it all out again, and goes through that until it gets through the last line of the stations file.

What I need to is essentially "loop" back through this whole thing again, so that the second line of the events file (file=1) is now being read and written out, and then do that for all the lines in the events file.

I have tried to use another "go to" command but my output never changes from what it spits out with the current code.

Does anyone know how to add another go to command so that it loops through this stuff again?

 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
2 Answers

I would avoid using goto at all - there's very rarely any need in modern Fortran and while I am far from a zealot they are generally seen in code that is more difficult to read. So here is what I would do - basically as you have two loops I would write two loops:

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$ 

Change the end= on the read from 2 to loop back to reading from 1. You will need to rewind 2 since you have reached the end.

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
Related