Comp Sci Read specific lines from a file using fortran90

AI Thread Summary
To read specific lines from a file in Fortran, the code must first skip lines before the desired range, as the file reading starts from the beginning. The user is attempting to read lines 5 to 10 from a file but is encountering issues with all output values being zero. It is suggested to modify the code to include a loop that reads and discards the first four lines before accessing the desired data. Additionally, ensuring that the values in the file are formatted correctly as real numbers (with decimal points) is important. Properly implementing these adjustments should yield the expected output.
shyvir3108
Messages
2
Reaction score
0

Homework Statement


I want to read only specific lines from a file using fortran code.

Homework Equations


Fortran code

The Attempt at a Solution


I have a dummy.txt file wrote
7
5
6
4
5
6
9
10
14
19

I want read only values between lines 5 to lines10. But when I use print statement, the value that produce is 0 in all lines. Can you advice what is wrong with my codes?

Below is my codes:
********************************************
Fortran:
subroutine average
implicit none
integer, parameter  :: t1 = 5 , t2 = 10
real                   :: sumEz
real,dimension(2000)   :: n1
integer             :: i,t
real                :: sum

sum = 0.0
sumEz = sum

open(2, file = 'add.txt', form='formatted')

open(unit = 12 , file = 'dummy.txt', status = 'old', action = 'read')

  do i = t1,t2

    read(12,*) n1(i)
    print *, i,'n1(i)=',n1(i)
 
    sumEz = sumEz + n1(i)**2
    write (2,*)sumEz
 
  end do

close (2)
close(12)

end subroutines

**********************************************

Result:

5 n1(i)=   0.00000000 
6 n1(i)=   0.00000000 
7 n1(i)=   0.00000000 
8 n1(i)=   0.00000000 
9 n1(i)=   0.00000000
10 n1(i)=   0.00000000
 
Last edited by a moderator:
Physics news on Phys.org
1) If FORTRAN tries to read a real, does it expect a decimal point? Try it with 7.0 5.0 etc.
2) If you only want to read lines 5-10, the index of n1 does not take care of that. You must actually read and skip the first 4 lines.
 
FactChecker said:
1) If FORTRAN tries to read a real, does it expect a decimal point? Try it with 7.0 5.0 etc.
2) If you only want to read lines 5-10, the index of n1 does not take care of that. You must actually read and skip the first 4 lines.

thank you for your response.

1. I have changed it to decimal, same output.
2. Index n1 is just to call the value in first column in dummy.txt.
3. My expected output should be like this:

6 n1(i)= 6.00000000
7 n1(i)= 9.00000000
8 n1(i)= 10.00000000
9 n1(i)= 14.00000000
10 n1(i)= 19.00000000
 
Last edited:
As @FactChecker wrote, you need to read all lines. This is the same for all programming languages when reading serial files: opening the file positions the input at the beginning of the file, and you must read in and discard the information until you reach the point of interest.

What you need is a loop from 1 to t1-1 that will read a dummy real.
 

Similar threads

Replies
7
Views
2K
Replies
2
Views
2K
Replies
2
Views
5K
Replies
10
Views
3K
Replies
2
Views
1K
Replies
7
Views
1K
Back
Top