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

Comments in Namelist Read From Character String Broken

Jeff_L_
Beginner
940 Views

Hi, I am not a Fortran expert by any means but have been searching for a solution to a particular problem that I'm having and have not had any success in finding a solution. Any help or feedback would be much appreciated.

I have a Fortran program that receives a character string from an external source (3rd party lib) that contains Namelist inputs. I have been using the READ statement to read the string directly into my namelist variables and all has been working well it seemed. However, recently I came along a use case where comments were embedded in the input string. All namelist inputs before the comment were read in correctly but all namelist inputs after the comment was ignored and the read statement returned a -1. I set up a small program to emulate the behavior and am including an example below. The namelist input string looks like the following:

$NLA

 a = 1,

 ! This is a comment

 b = 1,

$

---- Example Program to Emulate Problem ---

CHARACTER*255 temp

INTEGER(KIND=4) :: a, b, IOS

NAMELIST /NLA/ a, b

a = 0

b = 0

temp = "$NLA" // CHAR(10) // " a = 1," // CHAR(10) // " ! This is a comment, " // CHAR(10) // " b = 1," // CHAR(10) // "$"

READ(temp, NLA, IOSTAT=IOS)

--- End Program ---

Inexplicably to me, after the read, a=1 but b is still 0 and IOS = -1. It appears that the comment has somehow commented out the rest of the string. I thought this might have something to do with newlines and/or carriage returns needing to be in the right place in the string but have had no luck in getting anything to work. If I take out the comment, then b is set to 1 appropriately.

I also tried writing the string to a scratch file first and then reading in from the scratch file and this WORKS! Writing to a scratch file is not an ideal solution for me as I would definitely prefer to be able to find a solution directly with the string.

I have also thought about searching the string for the comments and removing them before I do the READ and this would be an acceptable solution. I thought I would ask the community first before implementing this, however, to see if I am confused. Thanks all.

-Jeff

0 Kudos
1 Solution
Steven_L_Intel1
Employee
940 Views

CHAR(10) in a string NAMELIST input has no meaning. For reads from CHARACTER variables, you get a new record only when you have an array, each element is considered one record. So if you want to use comments in this way you will have to break apart the string into individual array elements. As you have it, the comment excludes the rest of the value.

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
941 Views

CHAR(10) in a string NAMELIST input has no meaning. For reads from CHARACTER variables, you get a new record only when you have an array, each element is considered one record. So if you want to use comments in this way you will have to break apart the string into individual array elements. As you have it, the comment excludes the rest of the value.

0 Kudos
Jeff_L_
Beginner
940 Views

Ok that makes sense. Thanks!

-Jeff

0 Kudos
Reply