Stopping a FTN program

How do you do a dummy read (read*) then
end a FTN program and return command to the OS?

Not sure what you mean by a dummy read. You just want the program to wait until something is entered before terminating, basically just a pause, perhaps to keep a terminal window from closing?

open paws in Fortran Playground

program paws
implicit none
character(len=1) :: line
integer :: iostat
   write(*,'(a)',advance='no',iostat=iostat)'Enter to continue ...'
   read(*,'(a)',iostat=iostat) line ! pause until anything followed by a Return/Enter
   stop 'back to you, OS ...'
end program paws

This is how the program ends. But it won’t knock off completely. A cursor stays lingering about.

do while (stat .EQ.0).
do while (string(1:1).NE.'')
read(infile,“(a)”,iostat=stat)string
if(string==“”)print

call print_string(string,80)
end do
end do
end program expert

It looks like it is supposed to keep calling PRINT_STRING as long as you do not generate an error on input. I am not sure PRINT_STRING() does not do something useful but I suspect there is some history there, and that the code was reused or served some purpose like reading lines from a batch input file and copying it to output till an end-of-file; perhaps the end of the input file contained a description desired to have in the output.

Just guessing, but what you have is basically an infinite loop till an error occurs. Usually some key sequence will act as an end-of-file or error (usually on GNU/Linux and Unix that would be ‘ctrl-D’ or ‘ctrl-C’). I suspect ‘if(string==“”)print’ might have once been ‘if(string==“”)stop’ which would have made the program end on a blank line. If you want it to just stop all of it could be removed; if you want it to continue copying input until something specific is entered like a blank line or a keyword you could change the line ‘if(string==“”) print’ to something like ‘if(string==“stop”)stop’, or so on. But the explanation for why the program does not stop is that is an infinite loop until the read returns a non-zero status

as-is interactively you need to send it something like an end-of-file; if run with something like program.exe <input file the program would end when it hit end-of-file; but interactively it will just keep reading from the terminal window.

You are very kid to respond and offer your suggestions.

No. It is also said “to get a good answer first form a good question”. You need to supply information on what you want or expect the result to be, and supply things like the code sample without being asked. The first question is essentially unanswerable without asking for further information. You do not want others to have to guess what your question is, you want to pose one that gets the answer you need. An unclear question is understandable for newbies who may not yet have enough information to pose questions clearly but otherwise most of the onus is on the person asking the question to supply sufficient information for an answer to be formed.

1 Like

Yeah it would be very useful to include as much information as possible into the question, though it is sometimes difficult to make a “well-defined” question… (e.g. when the question is very broad)

Apart from that, maybe using infinite do + end do is more straightforward? Something like

program main
implicit none
integer stat, infile
character(50) string

open( newunit=infile, file="test.inp", status="old" )

do
    read( infile, "(a)", iostat=stat ) string
    if ( stat /= 0 .or. string == "" ) exit    !! stop reading if EOF or empty string found
    print *, "string = ", trim( string )
end do

close( infile )

end program

$ cat test.inp
hello
world
bye!

$ gfortran test.f90 && ./a.out
 string = hello
 string = world
 string = bye!

Instead, using the double do while (like in the OP’s code)

do while (stat == 0)
do while (string /= "")
    read( infile, "(a)", iostat=stat ) string
    print *, "string = ", trim( string )
end do
end do

gives an endless output like

 string = bye!
 string = bye!
 string = bye!
 string = bye!
 string = bye!
 string = bye!
 string = bye!
...

so the last line seems to be read repeatedly. If I change the read part as

do i = 1, 7
    read( infile, "(a)", iostat=stat ) string
    print *, trim( string ), stat
enddo

then it gives

 hello           0
 world           0
 bye!           0
 bye!          -1
 bye!        5001
 bye!        5001
 bye!        5001

so it looks like the test of EOF in the first do while loop is never executed… (because string is not empty and the second do while loop runs infinitely)

There are it-languages that will clean up at the end even if you have not closed down everything you should have when you left.

Thanks.

Septc, thank you for your suggestion. Much appreciated. I have the prog running okay now. I am only left with squiggles in the text that I cannot deal with.

1 Like