0

I have a data file containing 4 columns and many rows, I want to allocate the first column to t, second to x, third to y, and fourth to z. And have the data being read by row. So to imagine:

**T X Y Z** 
10, 1.0, 2.0, 3.0
20, 1.0, 2.0, 4.0

How would I allocate each variable to column? My program currently looks like: program arraycall

implicit none
integer, dimension(:,:), allocatable :: array
integer :: row
integer :: stat !Checking return values
!Define max. values of code, no.of rows
integer, parameter :: max_rows=1000
integer, parameter :: max_cols=4 !Maximum columns in array
! Total number of rows in file
integer :: tot_rows

allocate( array(max_cols,max_rows), stat=stat)
!Check return value
if (stat /=0) stop 'Cant allocate memory!'

open(10, file='C:\Users\matth\OneDrive\Desktop\Testfiles\vol.txt', &
access='sequential', status='old', FORM='FORMATTED')

DO row=1, max_rows
  !Directly read in arrays!
Read (10,*,iostat=stat) array(:,row)
if (stat>0) then
  stop 'An error occured while reading file'
  elseif (stat<0) then
  tot_rows = row-1
  print*, 'EOF reached. Found a total of',tot_rows, 'rows'


  exit
  endif
  END DO

  close (10)
  end program arraycall

So that it can read an 'x'amount of rows as I don't know exactly how many rows I'll have. How would I define each column to each variable and write this to a file or common block idea?

  • There is a variety of Qs and As hereabouts on the topic of reading arrays using Fortran. Maybe do a little more research. – High Performance Mark Feb 04 '20 at 16:37
  • 1
    As Mark says there are many questions like this here, But whatever you do don't use common blocks. Nobody should have used a common block in new code for 3 decades. – Ian Bush Feb 05 '20 at 03:36

0 Answers0