How to read always from the first line when using READ in a loop?

  • Thread starter Thread starter sky01237
  • Start date Start date
  • Tags Tags
    Line Loop
Click For Summary
SUMMARY

The discussion centers on the issue of reading from the first line of a file in Fortran during multiple iterations of a loop. The code provided demonstrates the use of the READ statement, which by default continues from the last read position. Rui seeks a solution to ensure that each iteration starts reading from the beginning of the file. A suggested solution is to utilize the REWIND statement, which resets the file pointer to the start of the file, allowing for repeated reads from the first line.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with file I/O operations in Fortran
  • Knowledge of loop constructs in programming
  • Basic understanding of subroutines in Fortran
NEXT STEPS
  • Research the Fortran REWIND statement and its usage
  • Explore file handling techniques in Fortran 90 and later versions
  • Learn about subroutine design and data flow in Fortran
  • Investigate best practices for managing file pointers in programming
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers working with file I/O, and anyone looking to optimize data reading processes in iterative algorithms.

sky01237
Messages
1
Reaction score
0
Hi, folks,
Here is the code:
SUBROUTINE CALCFG(N,X,F,G,ITER)
DOUBLE PRECISION X(N),G(N),F,A,d(5000),u(5000)
open (unit=2, file ="output.txt")
Do i = 1 , ITER - 1
Read( 2 , * )
End Do
Read( 2 , * ) d(ITER)
...


Do i = 1 , n - 1
Read( 2 , * )
End Do // it always starts from the next line of last iteration
How to make it start reading always from the first line for every iteration since in one iteration, maybe this subfunciton will be called several times and I want same data read in one iteration. Any help will be really appreciated. Thanks a lot.

Rui.
 
Technology news on Phys.org

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K