File reading problem

The casus is: in text asterisks delimit subsequent lines of text.
Under each asterisk there is a line with a key, identifying the code lines that follow, till a subsequent asterisk is encountered.

This code below extracts the keys and files them for reference. The code works okay.

But the code paced into the program that reads the data, blocks execution of that code which also uses the data file.

What is wrong?

!open(2,file=“keys.txt”,action=“write”)
!open(1,file=“data.txt”,action=“read”,iostat=stat)

!do
! read(1,)instring
! if(instring(1:1)=='
‘)then
! read(1,*)instring
! write(2,’(A)')instring
! end if
!end do

!close(1)
!close(2)

I have not followed all of this discussion in the other threads, but maybe the only problem is that you need to put quotes around the asterisk in the string comparison:

if ( instring(1:1) == '*' ) then

On a separate issue, I would recommend not using i/o units 1 and 2. Those are sometimes preconnected, and using those units in an open statement breaks that connection. Other parts of your code might need those preconnected units. Newer compilers have open statements that specify an unused unit number for you. For example

integer :: infile
!... other code here
open(newunit=infile,file=“data.txt”,action=“read”,iostat=stat)
!...other code here
read(infile,*) instring
!...

This way, you are sure not to reuse a unit number by mistake, or screw up preconnections, or anything like that. But even if your compiler doesn’t support that, you should use other unit numbers instead; pick ones above 10 just to be safe.

Something else that looks suspicious is that read(infile,*) instring does things a little differently than read(infile,'(a)') instring. If you really want to see what is in the first column of the input file, then you probably want that latter read statement, not the former. There might also be odd things that happen with asterisks and list-directed read anyway – asterisks are used as a repeat count delimiter in list-directed reads.

Ron, thank you very much for your interesting response. I will learn from it. Patrick

Ron, I want to thank you again for your response to my query, The things you write are not in the tutorials and books. One can spend days looking for errors, and not finding them without that knowledge. Patrick.

Here is a little example that demonstrates what I was talking about.

program ldio
   implicit none
   character(2) :: line = ' *'
   character :: instring
   read(line,*) instring
   write(*,*) 'ld read, instring=', instring
   read(line,'(a)') instring
   write(*,*) 'a read, instring=', instring
end program ldio

$ gfortran ldio.F90 && a.out
 ld read, instring=*
 a read, instring= 

Notice how the list-directed read skips over the initial space and looks for the first nonspace character. The (a) format doesn’t do that, it always looks at the first character in the line. This is done with internal i/o, but the same thing happens with file i/o. Of course, if you want to skip over any initial spaces, then list-directed i/o can be exactly what you need. Otherwise, you would need to read in the line and manually skip over the initial unwanted characters.

FYI: If you enter “```fortran” followed by lines of code

and terminated by a line simply composed of “```” your code will be displayed as-is with syntax highlighting:

program main
write(*,*)"*"
end program

which prevents the issues with the quotes and asterick and preserves indenting and so on.

I would like to thank those who have responded. I am considering this issue closed now.