Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Read array from a file in Fortran

  1. Mar 22, 2012 #1
    Hi every one. I had try to make a simple program but it doesn't work because i am an amateur

    The program as following:

    PROGRAM: Sum_row_n_col
    !
    ! PURPOSE: Read the 2-rank array from input disk file and calculate sums of all of the data in each row and each column in the array.
    ! The size of array will read from the first row of input disk file.
    !
    !****************************************************************************

    program Sum_row_n_col

    implicit none

    ! Data dictionary: Declare variable types and definitions
    Character (len=20) :: filename ! Name of input disk data file
    integer :: row ! The number of row in input array
    integer :: col ! The number of column in input array
    Real, allocatable, dimension (:,:) :: a ! Data array to be evaluated
    integer :: i, j ! Loop index
    integer :: status ! I/O status: 0 for success
    real, allocatable, dimension (:) :: sum_row ! Sums of data in each row in the array
    real, allocatable, dimension (:) :: sum_col ! Sums of data in each column in the array

    ! Initialize sums to zero
    sum_row = 0.
    sum_col = 0.

    ! Body of Sum_row_n_col
    ! Get the file name of the file containing the input data array
    Write (*,1000)
    1000 format ('Enter the file name containing input data: ')
    read (*,'(A20)') filename

    ! Open input data file. Status is OLD because the input data file must already exist.
    Open (Unit = 9, file = filename, status = 'old', action = 'read', iostat = status)

    ! Was opend was successful?
    fileopen: if (status == 0) then
    ! File was opened successfully, so read the data to process.
    ! Read the first row of input file data as the size of array

    Read (9,*, iostat = status) row, col



    ! Write out the size of input array
    Write (*,1010) row, col
    1010 format ('The size of array is: ', I2.0, 1x,'x', I2.0)


    ! Allocatable memory.
    Write (*,*) 'Allocating a: size = ', row, col
    Allocate (a(row,col), stat = status) ! Allocate memory of input array
    Write (*,*) 'Allcocating sum_row: size = ', row
    Allocate (sum_row(row), stat = status) ! Allocate memory of sums of each row
    Write (*,*) 'Allocating sum_col: size = ', col
    Allocate (sum_col(col), stat = status) ! Allocate memory of sums of each column

    ! Was allocation successful? If so, rewind file, read in data, and process it
    allocate_ok: if (status == 0) then
    Rewind (unit = 9) ! Rewind the file

    !Now read in the data. We know that was enough value to fill array.
    read (9,*) ((a(i,j), i = 1, row), j = 1, col) ! Get the value
    ! 1012 format (4(1x, F3.2))

    Write (*,1011) a
    1011 format (4(1x, F30.2))

    ! Calculate the sums of data in each row of array.
    row1: do i = 1, row
    row2: do j = 1, col
    sum_row (i) = a (i,j) + sum_row (i)
    end do row2
    end do row1

    ! Calculate the sums of data in each column of array
    col1: do j = 1, col
    col2: do i = 1, row
    sum_col (j) = a (i,j) + sum_col (j)
    end do col2
    end do col1

    ! Tell the user results
    Write (*,*)
    out1: do i = 1, row
    Write (*,1020) i, sum_row (i)
    1020 format ('Sum of row',I2, 1x, '=', F20.4)
    end do out1

    Write (*,*)
    out2: do j = 1, col
    Write (*,1030) j, sum_col (j)
    1030 format ('Sum of column', I2, 1x, '=', F20.4)
    end do out2

    ! Deallocate the array now that we are done
    Deallocate (a, stat = status)
    Deallocate (sum_row, stat = status)
    Deallocate (sum_col, stat = status)

    end if allocate_ok

    else fileopen

    ! Else file open failed. Tell user
    write (*,1080) status
    1080 format (1X,'File open failed -- status = ', I6)

    end if fileopen

    end program Sum_row_n_col
    ____________________________________________________

    Hope someones check it for me.
    Thanks
     
  2. jcsd
  3. Mar 23, 2012 #2

    jtbell

    User Avatar

    Staff: Mentor

    What exactly do you mean by "it doesn't work"? Does the compiler give you an error message when you try to compile it? If so, what is the error message?

    Or does it compile successfully, but it gives you an error message when you try to run it? If so, what is the error message?

    Or does it run but give you incorrect results? If so, can you describe how they are incorrect?
     
  4. Mar 23, 2012 #3
    This program can run but I get wrong result. When I "write" the input array on the screen, I see the program read the wrong array that is different with input file.

    It read also the first line, and blank space as element of array.
     
  5. Mar 29, 2012 #4
    Could any body help me to check my program? I still really don't understand why program read the wrong input array.

    Thank you very much!
     
  6. Mar 29, 2012 #5

    jtbell

    User Avatar

    Staff: Mentor

    I can't comment on whether you're doing the memory allocation properly because I've never used F90 and therefore never done memory allocation in Fortran.

    However, I do notice two things about your input statements which may be problems. First:

    read (9,*) ((a(i,j), i = 1, row), j = 1, col) ! Get the value

    I think this read-operation assumes that all the data is on one line. If the file has one row per line, you had best use a normal do-loop to cycle over the rows.

    Second: the first line of the file apparently contains the dimensions of the array. You first read that line and use the data to allocate your array, then you rewind the file. When you start to read data to fill the array, you read the first line again. Insert another read() statement immediately after the rewind(), to read that line again and put you at the beginning of the actual array data.

    [added] Now that I'm thinking about this again, why are you rewinding the file? After you read the first line and allocate the array, aren't you at the beginning of the array (in the file)?
     
    Last edited: Mar 29, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook