Read array from a file in Fortran

  • Context: Fortran 
  • Thread starter Thread starter volcano5683
  • Start date Start date
  • Tags Tags
    Array File Fortran
Click For Summary

Discussion Overview

The discussion revolves around a Fortran program designed to read a 2D array from a file and compute the sums of each row and column. Participants are exploring issues related to file reading, memory allocation, and the correctness of the output results.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster describes their program and its purpose, but indicates it is not functioning as expected.
  • One participant asks for clarification on what "it doesn't work" means, probing for specific error messages during compilation or runtime.
  • The original poster reports that the program runs but produces incorrect results, specifically mentioning that it reads a wrong array and includes blank spaces as elements.
  • Another participant suggests that the read statement may assume all data is on one line, recommending a loop to read rows if the file has one row per line.
  • This participant also points out that the first line of the file contains dimensions, and after reading it, the program rewinds the file, which leads to reading the dimensions again instead of the actual data. They suggest inserting another read statement after the rewind to correctly position the file pointer.
  • There is a question raised about the necessity of rewinding the file after reading the dimensions, as it may not be needed if the file pointer is already at the beginning of the data section.

Areas of Agreement / Disagreement

Participants express differing views on the file reading logic and memory allocation, with no consensus reached on the specific issues causing the incorrect results.

Contextual Notes

There are unresolved assumptions regarding the format of the input file and how data is structured within it, which may affect the program's ability to read the array correctly.

volcano5683
Messages
12
Reaction score
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
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?
 
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.
 
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!
 
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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
5K
  • · Replies 10 ·
Replies
10
Views
26K