1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Read specific lines from a file using fortran90

  1. Dec 18, 2017 #1
    1. The problem statement, all variables and given/known data
    I want to read only specific lines from a file using fortran code.

    2. Relevant equations
    Fortran code

    3. 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:
    ********************************************
    Code (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: Dec 18, 2017
  2. jcsd
  3. Dec 18, 2017 #2

    FactChecker

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
     
  4. Dec 18, 2017 #3
    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: Dec 18, 2017
  5. Dec 19, 2017 #4

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Loading...