Read specific lines from a file using fortran90

In summary: The code can be as follows:subroutine averageimplicit noneinteger, parameter :: t1 = 1 , t2 = 10real :: sumEzreal,dimension(2000) :: n1integer :: i,treal :: sumsum = 0.0sumEz = sumopen(2, file = 'add.txt', form='formatted')open(unit = 12 , file = 'dummy.txt
  • #1
shyvir3108
2
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 

FAQ: Read specific lines from a file using fortran90

What is the purpose of reading specific lines from a file in Fortran90?

The purpose of reading specific lines from a file in Fortran90 is to extract specific data or information from a file for further processing or analysis. This can be useful in various scientific and engineering applications, such as data analysis and simulation.

How can I read specific lines from a file in Fortran90?

To read specific lines from a file in Fortran90, you can use the READ statement with the UNIT and IOSTAT keywords to specify the file to be read and check for any errors. You can then use the FORMAT statement to specify the format of the data to be read and the READ statement with the END keyword to specify the end of the file.

Can I read multiple lines at once from a file in Fortran90?

Yes, you can use a loop and the READ statement to read multiple lines at once from a file in Fortran90. This can be done by specifying the UNIT and IOSTAT keywords as well as the number of lines to be read in each iteration of the loop.

How can I skip certain lines while reading a file in Fortran90?

To skip certain lines while reading a file in Fortran90, you can use the READ statement with the END keyword to specify the end of the line to be skipped. You can also use the IOSTAT keyword to check for any errors and handle them accordingly.

Is there a limit to the number of lines that can be read from a file in Fortran90?

There is no specific limit to the number of lines that can be read from a file in Fortran90. However, the maximum size of the file that can be read may be limited by the memory and processing capabilities of the computer system running the program.

Similar threads

Replies
7
Views
2K
Replies
2
Views
2K
Replies
2
Views
4K
Replies
10
Views
2K
Replies
16
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Back
Top