Read array from a file in Fortran

In summary, the program reads the two rank array from the input disk file and calculates the sums of all of the data in each row and each column in the array.
  • #1
volcano5683
12
0
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
 
Technology news on Phys.org
  • #2
volcano5683 said:
it doesn't work

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?
 
  • #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.
 
  • #4
Could anybody help me to check my program? I still really don't understand why program read the wrong input array.

Thank you very much!
 
  • #5
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:

1. How can I read an array from a file in Fortran?

To read an array from a file in Fortran, you can use the READ statement followed by the file name and the array name. For example: READ(UNIT=file, FMT=*, IOSTAT=ios) array. This will read the data from the file into the array.

2. What is the format of the file for reading arrays in Fortran?

The format of the file for reading arrays in Fortran depends on the data type and dimensions of the array. Each element of the array should be separated by a space or a comma. The file should also contain the same number of elements as the array.

3. How do I specify the dimensions of the array when reading from a file in Fortran?

You can specify the dimensions of the array in the READ statement by using the SHAPE keyword. For example: READ(UNIT=file, FMT=*, IOSTAT=ios) array(1:10, 1:10). This will read the data into a 10x10 array.

4. Can I read multiple arrays from the same file in Fortran?

Yes, you can read multiple arrays from the same file in Fortran by using the READ statement multiple times, each time specifying a different array name. You can also use the FORMAT keyword to specify the format of the data for each array.

5. How do I handle errors while reading an array from a file in Fortran?

You can use the IOSTAT keyword in the READ statement to handle errors while reading an array from a file in Fortran. This keyword will return an error code if there is an issue with reading the data from the file. You can then use an IF statement to handle the error and take appropriate action.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
605
  • Programming and Computer Science
Replies
6
Views
974
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top