Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

backspace with namelist read

kershaw
Beginner
432 Views

Hi, 

 

I have some code that makes use of backspace to print out an invalid namelist line. 

 

2021.5.0 20211109 works as expected, but 2021.8.0 20221119 prints the line after the invalid namelist line (If I put two backspace calls in, I get the same behavior as one backspace in 2021.5.0 20211109)

 

Here is a small example:

```

$ cat test_namelist_backspace.f90
program test_backspace

implicit none

integer :: ios, iunit
character(len=5) :: one, two, three, four, line
namelist /nums_nml/ one, two, three, four

one = 'nope'
two = 'nope'
three = 'nope'
four = 'nope'

open(newunit=iunit, file='input.nml', action='read', iostat=ios)
read(iunit, nml=nums_nml, iostat=ios)
print*, one, two, three, four

print*, 'iostat', ios
if (ios /= 0) then
backspace(iunit)
read(iunit, '(A)') line
print*, 'bad line: ', line
endif

close(iunit)

end program test_backspace

```

 

```

$cat input.nml
&nums_nml
one = 'one',
two = 'two',
cake = 'three'
four = 'four'
/

```

 

ifort (IFORT) 2021.5.0 20211109

 

```

ifort test_backspace_namelist.f90

$ ./a.out
one two nope nope
iostat 19
bad line: cake

```

 

ifort (IFORT) 2021.8.0 20221119

 

```

$ ifort test_namelist_backspace.f90
$./a.out
one two nope nope
iostat 19
bad line: four

```

 

Is backspace something I shouldn't rely on for namelist reads or is this a bug?

 

Cheers,

Helen

 

 

 

 

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
426 Views

You can't rely on this in general. The standard says two things relevant here:

1) If an error condition occurs during execution of an input/output statement, the position of the file becomes indeterminate.
2) if the statement is a READ statement or the error condition occurs in a wait operation for a transfer initiated by a READ statement, all input items or namelist group objects in the statement that initiated the transfer become undefined;

View solution in original post

2 Replies
Steve_Lionel
Honored Contributor III
427 Views

You can't rely on this in general. The standard says two things relevant here:

1) If an error condition occurs during execution of an input/output statement, the position of the file becomes indeterminate.
2) if the statement is a READ statement or the error condition occurs in a wait operation for a transfer initiated by a READ statement, all input items or namelist group objects in the statement that initiated the transfer become undefined;

kershaw
Beginner
425 Views

Thanks Steve, that is what I was thinking (but hoping not!).

0 Kudos
Reply