Reading file char by char - unexpected result

Hello,

I’m trying to read a line a from a file character by character in Fortran PGI using this snippet:

        
	character ax
	open(10,file='my_file.txt')
	do
		read(10,'(a)',advance='no',iostat=ios)ax
		print*,ios,ax
		if(ios.eq.0)then
			length=length+1
		else
			exit
		endif
	enddo
	close(10)

my_file.txt has on the first line a single record “nntest_small.csv”, apparently PGI Fortran reads a character and advances 1 place so the result of the above code is …
0 n
0 t
0 s
0 _
0 m
0 l
0 .
0 s
-2 s

My code works just fine with gfortran and Intel Fortran so I wonder if this a PGI bug or I did an error in my code,

The correct results should be:
0 n
0 n
0 t
0 e
0 s
0 t
0 _
0 s
0 m
0 a
0 l
0 l
0 .
0 c
0 s
0 v
-2


Thanks,

Renard

Hi Renard,

Can you please post a reproducing example? I tried to create one from the code snipit but the code worked as expected on Windows and Linux.

Example:

% cat test.f90 

      program foo 
   character ax
   open(10,file='my_file.txt')
   do
      read(10,'(a)',advance='no',iostat=ios)ax
      print*,ios,ax
      if(ios.eq.0)then
         length=length+1
      else
         exit
      endif
   enddo
   close(10) 
   end program foo
% cat my_file.txt 
nntest_small.csv
% pgf90 test.f90 -fast -V11.5
% a.out
            0 n
            0 n
            0 t
            0 e
            0 s
            0 t
            0 _
            0 s
            0 m
            0 a
            0 l
            0 l
            0 .
            0 c
            0 s
            0 v
           -2 v

I’ve used your code and I have the same error:

C:\test>pgfortran ftest.f90

C:\test>ftest
            0 n
            0 t
            0 s
            0 _
            0 m
            0 l
            0 .
            0 s
           -2 s

C:\test>

My machine is Windows 7 Professional, 64 bits.
I’ve tried the same code for reading pieces of 3 characters instead of one by one and consistently on my machine the code skips a character. I guess after each read it advances 1 position (I don’t know why).

   program foo 
   character(len=3)::ax 
   open(10,file='my_file.txt') 
   do 
      read(10,'(a3)',advance='no',iostat=ios)ax 
      print*,ios,ax 
      if(ios.eq.0)then 
         length=length+1 
      else 
         exit 
      endif 
   enddo 
   close(10) 
   end program foo

and the results:

C:\test>ftest
            0 nnt
            0 st_
            0 mal
            0 .cs
           -2 .cs

It will help if I will post the source code and the executable here ?

Renard[/code]

My version of PGI Fortran is:

PGI$ pgfortran -V

pgfortran 11.5-0 64-bit target on x86-64 Windows -tp nehalem
Copyright 1989-2000, The Portland Group, Inc.  All Rights Reserved.
Copyright 2000-2011, STMicroelectronics, Inc.  All Rights Reserved.
PGI$

Hi Renard,

This is odd since I’m also using PGI 11.5 on a Windows 7 64-bit system and it works fine for me. If you don’t mind, can you please send all your files to PGI Customer Service (trs@pgroup.com) and ask them to forward them to me? Hopefully I can then determine what the difference is.

Thanks,
Mat

Sure,

I will send the files from my personal email, the email used for registering my PGI Fortran license does not let to send executables as attachments.

The subject of my email (in case it will end up in your Spam box is “PGI Fortran bug for mkcolg”

Thanks

I’ve sent you the complete files : source code, input file, executable and all files pgfortran generates for debugging purposes.

Apparently PGI email server does not allow attachement with executables :) and I’ve received a reject email from the server.

I have no idea how to send you the files in safe way … maybe you can send me your email address in a private message ?

Renard

HI Renard,

I’ll send you a note on how to upload the files. Alternatively, you can try packaging all the files in a single ‘zip’ file. I think our mail server allows zip files through.

  • Mat

Hi Renard,

I just ran your executable on my system and got the correct output. My best guess is that there is something in your environment that is causing the difference, though I’m not sure what. We’ll correspond via email and hopefully understand what’s causing the difference.

  • Mat

Hello,

you have all my files, regarding this post, on your ftp site as a zip archive named “test_reading_file_char_by_char.zip”.

Hi Renard,

you have all my files, regarding this post, on your ftp site as a zip archive named “test_reading_file_char_by_char.zip”.

Correct. I’m able to get the correct output with your exe and data files. Meaning the difference is not the generated code but something in our environments.

I’m looking at your performance issue as well and will contact you via email once I have some more suggestions. Though in the mean time, you can try using the flags “-fast -Mipa=fast,inline -Mfprelaxed” which is roughly equivalent to Intel’s “/fast” flag. We are generally more conservative with regards to optimization that can impact numerical accuracy. Hence, -O4 does not include vectorization or relaxed floating point precision.

  • Mat