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

Error reading namelist

Clive_J_1
Beginner
2,334 Views

Hi.

I have a bizarre error which is shown by the skeleton code below.

Basically, I have a character variable which I have placed with a namelist. I initilaise the string and write the namelist to a file. I rewind the file and read the namelist. It works fine if the length of the character string is 122 but if I put it to 123 (as in the code) it fails with IOSTAT=18 which is 'too many values for NAMELIST variable'.

I am using Intel Fortran XE 12.0.4.196 in VS2010

In the full program the character string will be much longer so I did some trial and error to see what was the critical point and as far as i know, 122 has no significance whatsoever.

I have tried setting CARRIAGECONTROL='NONE' and BLOCKSIZE=123' on the OPEN statement but doesn't make any difference.

 

Code also works fine with gfortran.

Any ideas?

PROGRAM MAIN

! Program to test namelist read

IMPLICIT NONE

CHARACTER(LEN=123) :: CString
CHARACTER(LEN=150) :: Message

NAMELIST /TestNL/CString

INTEGER :: Ios
INTEGER :: I

OPEN(6,file='stdout.txt',form='formatted')

OPEN(10,file='test.nl',form='formatted')

DO I=1,123
CString(I:I) = 'A'
ENDDO

WRITE(10,TestNL)

REWIND(10)

READ(10,TestNL,IOSTAT=Ios,IOMSG=Message)

write(6,*) 'ios ',ios
if(ios /= 0) write(6,*) 'Message: ',Message
write(6,*) CString


STOP
END

0 Kudos
4 Replies
lklawrie
Beginner
2,334 Views
Did you look at the file produced (test.nl) and see that it looks as you expect? Also, did you try closing and reopening the file (just to make sure everything is written).
0 Kudos
Clive_J_1
Beginner
2,334 Views
Yes, the file is as expected. It appears that the input is being truncated at 122 characters. My suspicion is that there is some kind of record length parameter which I need to override.
0 Kudos
Steven_L_Intel1
Employee
2,334 Views
The standard says NOTE 10.40 Namelist output records produced with a DELIM= specifier with a value of NONE and which contain a character sequence might not be acceptable as namelist input records. The problem here is that the default for NAMELIST and list-directed output is DELIM='NONE' which means that character strings are not delimited. With both kinds of formatting, it is processor-dependent on where any line breaks occur, and when reading in a long string that spans records, an undelimited string won't be read correctly. The fix here is to add DELIM="QUOTE" (or "APOSTROPHE") to the OPEN of unit 10.
0 Kudos
Clive_J_1
Beginner
2,334 Views
Thanks Steve, you're a star. I guessed it would be something like that but couldn't work out what. Clive
0 Kudos
Reply