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

  • Fortran
  • Thread starter aLostProgramm
  • Start date
  • Tags
    Fortran
In summary: PROGRAM testThis is my dataset. The data is separated with a space in each row.21 23 40 41 7853 10 79 25 3642 28 17 30 9911 22 33 44 55And this is my output:2123404178531079253642281730991122334455
  • #1
aLostProgramm
2
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
  • #2
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:
  • #3
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
 

1. What is Fortran 90?

Fortran 90 is a high-level programming language commonly used in scientific and engineering applications. It was released in 1991 and is an updated version of the original Fortran language.

2. Why is it called "silly"?

The term "silly" in this context is used to indicate that the question being asked is not serious or complex, but rather a simple or humorous one related to Fortran 90.

3. Is Fortran 90 still relevant today?

Yes, Fortran 90 is still relevant today, especially in scientific and engineering fields. It is known for its high performance and efficiency in handling complex mathematical and scientific computations.

4. What is the difference between Fortran 90 and other versions of Fortran?

Fortran 90 introduced many new features and enhancements compared to previous versions of Fortran. This includes dynamic memory allocation, modules, recursion, and improved support for arrays, strings, and complex numbers.

5. Can Fortran 90 be used for non-scientific applications?

While Fortran 90 is primarily used in scientific and engineering applications, it can also be used for other purposes. However, there may be other programming languages that are better suited for non-scientific applications.

Similar threads

  • Programming and Computer Science
Replies
4
Views
620
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top