Reading input files in FORTRAN

Purpose: to create a program that takes two separate files, opens and reads them, assigns their contents to arrays, does some math with these arrays, creates a new array with product numbers, prints to a new file. Simple enough?

My input files have comment characters at the beginning. One problem is that they are "#" which are comments for most graphics programs, but not FORTRAN. What is an easy way to tell the computer not to look at these characters? Since I do not have previous FORTRAN experience, I am plowing through this with two test files. Here is what I still have:

PROGRAM gain
  IMPLICIT NONE
  REAL, DIMENSION (1:4, 1:8)     :: X, Y, Z
  OPEN(1, FILE='test.out', &
        STATUS='OLD', ACTION='READ')            ! opens the first file
  READ(1,*), X
  OPEN(2, FILE='test2.out', &
    STATUS='OLD', ACTION='READ')            ! opens the second file
  READ(2,*), Y
  PRINT*, X, Y

  Z = X*Y
!  PRINT*, Z
  OPEN(3, FILE='test3.out', STATUS='NEW', ACTION='WRITE')   !creates a new file
  WRITE(3,*), Z
  CLOSE(1)
  CLOSE(2)
  CLOSE(3)
END PROGRAM

PS. , gobblety gook. . , -. .

+3
3

, , . IF, , "#" . "#", . , .

+3

, , - - , . -. : , , ...... "backspace" , , . , - , , .

... , "#" . : "" - . , - - .

P.S. "Fortran" Fortran 90 - "FORTRAN" FORTRAN 77 .

module read_file_module

   implicit none

contains

   subroutine read_file (UnitNum, FileName, NumRows, NumCols, Array )

      integer, intent (in) :: UnitNum
      character (len=*), intent (in) :: FileName
      integer, intent (in) :: NumRows, NumCols
      real, dimension (1:NumRows, 1:NumCols), intent (out) :: Array

      character (len=300) :: line
      integer :: i, j

      open (unit=UnitNum, file=FileName, status='old', action='read' )

      ReadComments: do
         read (UnitNum, '(A)') line
         if (line (1:1) /= "#") exit ReadComments
      end do ReadComments

      backspace (UnitNum)

      do i=1, NumRows
         read (UnitNum, *) (Array (i, j), j=1,NumCols)
      end do

      close (UnitNum)

      return

   end subroutine read_file

end module read_file_module 




program test_prog

use read_file_module

implicit none

real, dimension (1:8, 1:4) :: Array
integer :: i, j

call read_file (66, 'TestFile.txt', 8, 4, Array)

do i=1, 8
  write (*, '( 4(2X, F7.3) )' ) (Array (i, j), j=1,4)
end do

end program test_prog

, , :

#  comment one
#  comment two
1.1   2.0  3.0  4.1
1.2   2.0  3.0  4.2
1.3   2.0  3.0  4.3
1.4   
  2.0  3.0  4.4
1.5   2.0  3.0  4.5
1.6   2.0  3.0  4.6


1.7   2.0  3.0  4.7
1.8   2.0  3.0  4.8
+6

, FORTRAN 77, ( , ). ( ):

1   REAL FUNCTION myfile(unit, file, rows, columns)
2   IMPLICIT NONE
3   INTEGER, INTENT(IN) :: unit, rows, columns
4   CHARACTER(LEN=*) :: file
5   REAL, DIMENSION (1:columns, 1:rows) ::X
6   OPEN(unit, FILE=file, STATUS='OLD', ACTION='READ')
7   READ(unit,*), X
8   PRINT*, X         
9   CLOSE(unit)
10  myfile= 0
11  END FUNCTION myfile
12
13  PROGRAM gain
14  errno = myfile(1, "test.out", 8, 4)
15  END PROGRAM

:

  • 6 - "" FILE = 'file. , file , .
  • 10 - , , . 0, . , , , , . ( X myfile) . ( FORTRAN 77 , , Fortran).
  • 14 - . , gfortran . .
  • 14 () - (test.out) . , (, , , .)

myfile . , . 10 "" . 14 :

call myfile(1, "test.out",8,4)

EDIT: , , . , - SO, . , .

. , ( , , ). / /:

  • , ( ), , , , . , . , , . , , . , .
  • , , , #. . , , reset , # 1 , . , # 1, , . .
  • , #, , . , . ( ) .

Which method you choose (and other people may have other suggestions) depends on your specific application. Good luck.

+1
source

Source: https://habr.com/ru/post/1744987/


All Articles