Fortran Read array from a file in Fortran

Click For Summary
The discussion revolves around a Fortran program intended to read a two-dimensional array from a file and calculate the sums of its rows and columns. The user reports that the program does not work correctly, specifically stating that it reads an incorrect array, including the first line and blank spaces as elements. Key issues identified include the way the program reads the input data. It uses a single read statement that assumes all data is on one line, which may not be the case if the file has one row per line. Additionally, after reading the dimensions of the array and allocating memory, the program rewinds the file but fails to read the first line again before attempting to read the actual data. This oversight leads to incorrect data being processed.Suggestions for resolution include modifying the read operation to correctly handle the input format and ensuring that the program properly skips the first line after reading the dimensions. These adjustments are crucial for the program to function as intended and produce accurate results.
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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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