This must be a stupid question, sorry, but I couldn’t find a way to check for just pressing return, and not entering anything, which I don’t want. Checking for entry == “”, for status, and strlen, didn’t do the trick.
This code distingushes between nonspace strings and strings with only spaces (i.e. blanks in fortran terms) in them, but it does not distinguish between a <cr>
and one or more spaces followed by the <cr>
. That is probably sufficient to answer the original question, but I’m wondering if there is a way to use BLANK= somehow to distinguish also between a <cr>
and the lines with various numbers of spaces.
Thank you for your code, I tried it, entering return does not terminate the program. This is what I did, following your advice:
do
read(*,‘(A)’) seek
if (len_trim(seek) /= 0) exit
end do
Hi Ron, so what is the way to terminate execution if only return is pressed?
Try this slight modification of @kargl 's code
program foo
implicit none
character(len=80) str
read(*,'(a)') str
if (len_trim(str) /= 0) then
print *, 'valid input, str: ',trim(str),', len_trim(str): ',len_trim(str)
else
print *, 'empty str, exiting...'
end if
end program foo
Yes - your code does the job. Thank you both.
read(,‘(a)’) seek
if (len_trim(seek) == 0) then
call locate(4,3)
print,“Press Enter to leave.”
read*
stop
end if
I think the question of how to distinguish between a null line and a line composed of blanks was not addressed; although it is unusual to distinguish between them. One way is the SIZE= parameter
program foo
implicit none
character(len=80) str
integer :: isz, iostat
do
write (*, '(a)', advance='no') 'enter value ..'
read (*, '(a)', size=isz, advance='no', iostat=iostat) str
if ( str =/ '' ) then
print *, 'valid input, str: ', trim(str), ', len_trim(str): ', len_trim(str), 'size=', isz
elseif (isz .eq. 0) then
print *, 'null str, exiting...'
stop
else
print *, 'blank string, size...', isz
end if
end do
end program foo