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
Click For Summary
SUMMARY

The discussion focuses on the syntax for reading datasets in Fortran 90, specifically addressing issues encountered by a C/C++ programmer transitioning to Fortran. The user attempted to read a dataset into a one-dimensional array using the format specifier "5I3", which is incompatible with the single integer variable X(I). The correct approach involves using "READ(1, *) X(I)" or employing an implied DO loop for more efficient data reading. The suggested solution resolves the output issue, where only the last few values were printed correctly.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with reading files in Fortran
  • Knowledge of array handling in Fortran
  • Basic understanding of format specifiers in Fortran
NEXT STEPS
  • Learn about Fortran 90 file I/O operations
  • Explore the use of implied DO loops in Fortran
  • Study Fortran 90 array manipulation techniques
  • Investigate advanced format specifiers for reading data in Fortran
USEFUL FOR

This discussion is beneficial for programmers transitioning from C/C++ to Fortran, particularly those working with data input and output in scientific computing contexts.

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
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K