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

  • Thread starter sky01237
  • Start date
  • Tags
    Line Loop
In summary, the conversation discusses a code for a subroutine called CALCFG and the issue of reading data from a file for each iteration. The suggestion is to use the REWIND statement to start reading from the first line for every iteration.
  • #1
sky01237
1
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
  • #3


Dear Rui,

Thank you for sharing your code and question. It seems like you are trying to read data from a file in a loop, but the loop is not starting from the first line of the file every time. This could be due to the fact that the file pointer is not being reset to the first line before each iteration.

To solve this issue, you can use the "rewind" function to reset the file pointer to the beginning of the file before each iteration. This will ensure that the data is read from the first line every time the loop is executed. Your code could look something like this:

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
rewind(2) // reset file pointer to the beginning of the file
Read( 2 , * )
End Do // it will now start reading from the first line every time

I hope this helps. Good luck with your research!

Best,
 

1. How do I ensure that I always read from the first line when using READ in a loop?

To read from the first line in a loop, you can use the SEEK function to move the file pointer to the beginning of the file before each READ operation. This will ensure that each time the loop runs, the first line will be read.

2. Can I use a different method to read from the first line in a loop?

Yes, instead of using the SEEK function, you can also use the RESET function to reset the file pointer to the beginning of the file before each READ operation. This will have the same effect as using SEEK.

3. Do I need to use a specific type of loop when reading from the first line?

No, you can use any type of loop (e.g. FOR, WHILE) when reading from the first line. Just make sure to include the appropriate code (either SEEK or RESET) before each READ operation to ensure you are always starting from the first line.

4. Will using SEEK or RESET affect the performance of my code?

In most cases, using SEEK or RESET to read from the first line in a loop should not significantly impact the performance of your code. However, if you are working with very large files, it is recommended to test and compare the performance of both methods to determine which one is more efficient for your specific case.

5. Is there a way to read from the first line without using SEEK or RESET?

Yes, you can use the READLN function to read a line from the file and then immediately use the RESET function to move the file pointer back to the beginning before the next READ operation. Keep in mind that this may impact the performance of your code, especially for larger files.

Similar threads

  • Programming and Computer Science
Replies
5
Views
978
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
8
Views
782
  • Programming and Computer Science
Replies
4
Views
568
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
925
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
960
Back
Top