7 Replies Latest reply on Jul 23, 2008 5:42 PM by 807575

    reading Windows text file in Fortran?

    807575
      Is there a way to make the f90 compiler read Windows text files properly? I made a simple test with a text file created on a Windows system containing these three lines:

      This is a Windows text file.
      It has some extra control characters.
      Unix does not have those.

      The output I got was:

      <-ext file.
      <-e extra control characters.
      <-se.

      Kevin
        • 1. Re: reading Windows text file in Fortran?
          807575
          I don't understand what you are asking. The compiler itself reads Windows
          text files without a problem. For example, the program

          PROGRAM MAIN^M
          CHARACTER*80 LINE^M
          DO^M
          READ (*, '(A)', END= 99999) LINE^M
          PRINT '(A)', TRIM(LINE)^M
          END DO^M
          99999 STOP^M
          END^M

          where ^M is the carriage return character compiles without a problem.
          The text file text.txt contains

          This is a Windows text file.^M
          It has some extra control characters.^M
          Unix does not have those.^M

          When I redirect that file to be the input to the program above, I get
          the same sequence of characters as output.

          Bob Corbett
          • 2. Re: reading Windows text file in Fortran?
            807575
            Hi Bob,

            Sorry, perhaps I was unclear. I didn't mean the compiler having a problem reading the Windows text file, I mean the resulting Fortran program being unable to read a Windows text file.

            I have a text file created on Windows (windows.txt) which contains, as you say, the extra CONTROL-M characters from the point of view of a Unix machine:

            This is a Windows text file.^M
            It has some extra control characters.^M
            Unix does not have those.^M

            My program is trying to read the text file. For example,

            program fortranread

            character*60 myline
            character*60 winfile
            integer i

            winfile='windows.txt'

            open(unit=1, file=winfile, status='old')
            10 format(a)
            do i=1,3
            read(1,10) myline
            write(*,*) 'winfile is ->',myline,'<-'
            enddo
            close(1)

            end

            When I compile and run the program, I get this output:

            <-ext file.
            <-e extra control characters.
            <-se.
            • 3. Re: reading Windows text file in Fortran?
              807575
              That makes more sense. The problem isn't with the Fortran program, but with the Unix terminal (xterm, or whatever you're using to display the output). If you pipe the output through od like this you'll see all the characters, including the ^M:
              bash-3.00$ fortranread | od -c
              0000000       w   i   n   f   i   l   e       i   s       -   >   T   h
              0000020   i   s       i   s       a       W   i   n   d   o   w   s    
              0000040   t   e   x   t       f   i   l   e   .  \r                    
              0000060                                                                
              0000100                                           <   -  \n       w   i
              0000120   n   f   i   l   e       i   s       -   >   I   t       h   a
              0000140   s       s   o   m   e       e   x   t   r   a       c   o   n
              0000160   t   r   o   l       c   h   a   r   a   c   t   e   r   s   .
              0000200  \r                                                            
              0000220                               <   -  \n       w   i   n   f   i
              0000240   l   e       i   s       -   >   U   n   i   x       d   o   e
              0000260   s       n   o   t       h   a   v   e       t   h   o   s   e
              0000300   .  \r                                                        
              0000320                                                                
              0000340                   <   -  \n
              0000347
              When the terminal sees the ^M character, it does a carriage return. That means that the next character gets written in the first column. Since the characters after the ^M are spaces, that makes the beginning of the lines appear blank.

              If you want to see the output you expect, you need to pipe it through "tr -d '\015'", or you need to make the Fortran program itself delete the ^M characters. For example, you could write "myline(1:len_trim(myline)-1)" to always strip the last character of the line.

              We should consider making formatted reads strip the ^M by default. I don't know whether any program would actually want them.
              • 4. Re: reading Windows text file in Fortran?
                807575
                Hi,

                thanks, that analysis helps. I can't blindly strip away the last character since the file I am reading might be created in Windows, or it might be created in Unix. I can put something like this in though

                if (myline(len_trim(myline):len_trim(myline)).eq.char(13)) then
                myline(len_trim(myline):len_trim(myline))=' '
                endif

                For the record, the Absoft Fortran compiler and the Intel Fortran compiler appear to strip the ^M. It would certainly be helpful if there was a compiler switch to toggle the ^M.
                • 5. Re: reading Windows text file in Fortran?
                  807575
                  Hi,

                  while this does help with the situation where I am reading a text string, when it comes to reading numerical quantities, there are still problems.

                  Consider the extension of this Windows text file to

                  This is a Windows text file.^M
                  It has some extra control characters.^M
                  Unix does not have those.^M
                  1 2 3^M
                  2.71 3.14 1.23456^M
                  done^M

                  Now insert these lines into the test program

                  read(1,*) i1,i2,i3
                  write(*,*) 'integers are ',i1,i2,i3
                  read(1,*) r1,r2,r3
                  write(*,*) 'reals are ',r1,r2,r3
                  read(1,10) myline
                  write(*,*) 'winfile is ->',myline,'<-'

                  When you run the program, it will now crash with

                  ****** FORTRAN RUN-TIME SYSTEM ******
                  Error 1083: unexpected character in integer value
                  Location: the READ statement at line 24 of "fortran.f"
                  Unit: 1
                  File: windows.txt
                  Input: 3?
                  ^
                  • 6. Re: reading Windows text file in Fortran?
                    807575
                    Am I dead in the water if I want to continue using Fortran to parse a text input file generated by Windows?
                    • 7. Re: reading Windows text file in Fortran?
                      807575
                      I'm not aware of any workaround for the numeric input (other than to run dos2unix on the file). You could file a bug using bugs.sun.com.