What is the Syntax for Reading a Dataset in Fortran 90?

  • Context: Fortran 
  • Thread starter Thread starter aLostProgramm
  • Start date Start date
  • Tags Tags
    Fortran
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
aLostProgramm
Messages
2
Reaction score
0
I feel somewhat embarassed to ask for help concerning this. I am a C/C++ programmer, but I am currently learning Fortran 90 on the fly and in a hurry. I am going to have to read in a 32x25600 dataset into a 1-d array (I know, not especially efficicent). I decided to start with a more simplistic 5x4 dataset into a 1-d array, and would build from there. However, the unique syntax of Frotran 90 is confusing me. Below is my source code:

program test
IMPLICIT NONE

INTEGER I,var
CHARACTER*10 output
INTEGER, PARAMETER :: N=20
INTEGER, DIMENSION(N) :: X

OPEN (1,FILE="fake.dat",STATUS="OLD")

DO I=1,N
READ(1,"(5I3)",end=10) X(I)
END DO

10 do I=1,N
print *, X(I)
end do

END PROGRAM test

This is my dataset. The data is separated with a space in each row.
21 23 40 41 78
53 10 79 25 36
42 28 17 30 99
11 22 33 44 55

And this is my output:
11
22
33
44
55
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

I promise that I am not a student trying to get homework help. I am currently sitting in my cubicle trying to resist the temptation of punching my monitor out of frustration. Thanks in advance for the help.
 
Last edited:
Physics news on Phys.org
aLostProgramm said:
I feel somewhat embarassed to ask for help concerning this. I am a C/C++ programmer, but I am currently learning Fortran 90 on the fly and in a hurry. I am going to have to read in a 32x25600 dataset into a 1-d array (I know, not especially efficicent). I decided to start with a more simplistic 5x4 dataset into a 1-d array, and would build from there. However, the unique syntax of Frotran 90 is confusing me. Below is my source code:

program test
IMPLICIT NONE

INTEGER I,var
CHARACTER*10 output
INTEGER, PARAMETER :: N=20
INTEGER, DIMENSION(N) :: X

OPEN (1,FILE="fake.dat",STATUS="OLD")

DO I=1,N
READ(1,"(5I3)",end=10) X(I)
END DO

10 do I=1,N
print *, X(I)
end do

END PROGRAM test

This is my dataset. The data is separated with a space in each row.
21 23 40 41 78
53 10 79 25 36
42 28 17 30 99
11 22 33 44 55

And this is my output:
11
22
33
44
55
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

I promise that I am not a student trying to get homework help. I am currently sitting in my cubicle trying to resist the temptation of punching my monitor out of frustration. Thanks in advance for the help.

Welcome to Physics Forums!

Your format specifier for the read doesn't agree with the variable you're using. The format expression, 5I3, indicates that you are reading 5 integer values. The variable X(I) can hold only one integer value.

Try this:
READ (1, *) X(I)

On a side note, you are printing the values out one-by-one, but attempting to read them in five at a time.
 
Last edited:
Mark44 said:
Your format specifier for the read doesn't agree with the variable you're using. The format expression, 5I3, indicates that you are reading 5 integer values. The variable X(I) can hold only one.

Try this:
READ (1, *) X(I)

You could also use an implied DO loop with the READ statement. The following code works with my compiler.

Code:
      IMPLICIT NONE

      INTEGER I,var
      CHARACTER*10 output
      INTEGER, PARAMETER :: N=20
      INTEGER, DIMENSION(N) :: X

      OPEN (1,FILE="fake.dat",STATUS="OLD")
      READ(1,*) (X(I),I=1,N)

      DO I=1,N
            print *, X(I)
      END DO
      END