Topic: read statement hangs

My code reads character input from the console, this works fine, but if you hit enter without entering any data the read hangs and the only way out is to do a cntrl-c. I have tried iostat=, err=, eor=, size= (with advance='mo') ... but I have not found a way to detect a carriage return.

     read (*,1) line
1   format (80a1)


Thanks, Imre

2 (edited by designer 2022-04-15 02:10:37)

Re: read statement hangs

Though it's probably overkill and superstition, I'd assign something to Line first, even if it's just Line = "" or Line = " ",
then I'd put a Write, just under the Read, to write out Line and test it with data content. Finally, just hit the enter key as before. I do understand that it hangs after the Read with just Enter, you may not get to the Write in that case.

I'm suggesting those steps because it more clearly brackets the issue. And it would be interesting to see if hitting the space bar and Enter works too. I mean a <space> in this context is just as fine as a letter or other data.

Also, I'm suspicious of that 80a1 format. Can you make it more generic - What does Read(*,'(a)') line do in its place (with data and with just an enter? or try 1a1 and test with one character.

Are you using a Mac or PC Platform?

I would have tired some of my suggestions first, but I can't just copy the code you provided and run it here. It doesn't like the
Read(*,1) line
1 format(80a1)
construction. So I'd need your I/O statement in there too - something so I can duplicate the same success/error result.

Re: read statement hangs

Imre,

I notice a couple things about your example.  First, your format is looking for 80 single characters.  When I use your format statement and enter text, I actually only get the first letter stored.  You might instead want to use the more generic '(A)' text format for the read to make sure it reads all text up to the new line.

Regardless, I'm not seeing a hang.  My code:

program cr
implicit none

    character(50)::line
    
    Print *, "Enter some text:"
    Read(*, '(A)') line
    
    Print *, trim(line), len_trim(line)
    
end program cr

seems to correctly register a string of zero length when I simply press Enter.  Does the above work for you?

Jeff Armstrong
Approximatrix, LLC