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

  • Thread starter Thread starter aLostProgramm
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion centers on a programmer transitioning from C/C++ to Fortran 90, facing challenges with syntax while attempting to read a dataset into a 1-D array. The user shares their initial code, which attempts to read a 5x4 dataset but results in incorrect output, displaying only the last row of data and many zeros. The main issue identified is the mismatch between the format specifier used in the READ statement and the variable being populated. The suggested solution is to simplify the READ statement to accommodate the data format, using "READ (1, *) X(I)" or employing an implied DO loop to read all values at once. This approach is recommended to ensure proper data handling and output.
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:
Technology 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
4
Views
2K
Replies
20
Views
3K
Replies
3
Views
2K
Replies
5
Views
5K
Replies
4
Views
2K
Replies
8
Views
4K
Replies
8
Views
2K
Back
Top