2

I am writing a Fortran application, and I get this problem. When I define a namelist as following:

CHARACTER(100) :: INPUT_DIR, OUTPUT_DIR, ROOT_DIR    
NAMELIST /IODIR/ INPUT_DIR, OUTPUT_DIR

and then I read IODIR from file as:

READ(FUNIT,IODIR, ERR=99)

The data in file is:

&IODIR INPUT_DIR="Input", OUTPUT_DIR="Output" /

But it get error

"End of file".

It seems like the length of variables is longer than their defined in file. I don't know how to set delimiter for the character variable, or read an unknown character in namelist. I use GNU Fortran to build.

  • are you sure that read is causing the error? It looks ok and even if there was a problem you would not get an error but rather you would branch to label 99. – agentp Nov 16 '17 at 19:48
  • 1
    @agentp `err=` is for branching on error conditions only. – francescalus Nov 16 '17 at 20:10
  • @francescalus right.. its been a long time since I used them. I thought `err` would also catch the EOF (which is an error after all). In any case based on testing (intel&gnu), even if there is not a newline at the end of the line, the shown code will not produce and end of file error. – agentp Nov 16 '17 at 22:04
  • Trying to read beyond the end of a file triggers an end-of-file condition, @agentp, not an error condition. – francescalus Nov 16 '17 at 22:08
  • @francescalus is correct - EOF is not an error, per the standard. I know some older compilers I worked on got this wrong, – Steve Lionel Nov 17 '17 at 02:11
  • @SteveLionel my mistake acknowledged (again). Fact remains there doesn't seem to be any way for the example shown to generate an end of file message. – agentp Nov 17 '17 at 15:56
  • @agentp sure there is. You'll get an EOF if a matching NAMELIST group, or the closing / is not seen. As is often the case, I predict that the snippets shown do not reflect the actual program or data being used. I created a test program based on what is shown here and it worked fine. – Steve Lionel Nov 18 '17 at 01:33
  • well of course if the file contents are not what he says they are then anything goes – agentp Nov 18 '17 at 02:27
  • 4
    I found that, when the program reads the last namelist in file, it reaches to the end of file. If we add one more line in the EOF (can be an empty line), program will pass. Thank all very much! – Hải Phạm Thanh Nov 20 '17 at 04:32

1 Answers1

0

I had the same problem with the online gfortan compiler, same result from an installed version. This problem is all over the web, so on the basis of using reliable sources I: Installed gfortran in Windows 10 Bash - all good. Installed gfortran in Cygwin - all good.

  • 2
    Notice this comment by the OP: *"I found that, when the program reads the last namelist in file, it reaches to the end of file. If we add one more line in the EOF (can be an empty line), program will pass."* This is likely the key. Some GCC versions will accept such a file, some won't. The portable solution is to not end the file suddenly by an end-of-file marker. – Vladimir F Героям слава Dec 19 '18 at 12:07