Read specific lines from a file using fortran90

Click For Summary

Discussion Overview

The discussion revolves around reading specific lines from a file using Fortran 90, particularly focusing on how to correctly read values from a text file and troubleshoot issues related to output being zero. The scope includes programming techniques and debugging in Fortran.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to read specific lines from a file but encounters an issue where all output values are zero.
  • Another participant suggests that Fortran may require decimal points for real numbers and recommends trying values like 7.0 and 5.0.
  • It is noted that the index used to access the array does not account for skipping the first four lines, which may lead to incorrect data being read.
  • A later reply emphasizes that when reading from files, all lines must be read until reaching the desired point, suggesting a loop to discard initial lines before reading the target lines.

Areas of Agreement / Disagreement

Participants generally agree on the need to read all lines up to the desired point, but there is no consensus on the specific implementation details or the exact cause of the zero output issue.

Contextual Notes

There are limitations regarding the assumptions about file structure and the handling of data types in Fortran, which may affect the reading process. The discussion does not resolve these issues.

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 ·
Replies
7
Views
2K
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K