Reading a file repeatedly in each iteration of a do loop in fortran

Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming issue related to reading an input file repeatedly within a loop. Participants explore methods to read from the beginning of a file in each iteration of a do loop, addressing challenges with the "rewind" statement and file handling practices.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks guidance on how to read an input file from the beginning in each iteration of a do loop, mentioning that the "rewind" statement did not work as expected.
  • Another participant suggests a nested loop structure to read the file multiple times, but acknowledges a lack of expertise.
  • A suggestion is made to close and reopen the file if "rewind" fails, indicating potential operating system restrictions on file operations.
  • Participants discuss the error message indicating that the program cannot find the file associated with unit 2, speculating that the use of double quotes in the "open" statement might be an issue.
  • There are concerns about the structure of the provided code, including unassigned variables in read statements and the placement of the end program statement.
  • Some participants argue that using "rewind" is more efficient than closing and reopening the file, while others point out that closing the file loses the connection to it.
  • One participant mentions that using units 1 to 7 for file operations is discouraged, suggesting that units 10 and above should be used instead.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of "rewind" versus closing and reopening the file. There is no consensus on the best approach to resolve the file reading issue, and multiple competing suggestions are presented.

Contextual Notes

Some participants note that the error messages may stem from the use of specific file units and the syntax of the "open" statement. There are also unresolved questions regarding the structure and logic of the provided code, which may affect its execution.

fort_phys
Messages
3
Reaction score
0
Hi everybody,
I have just joined this forum and I have a problem in fortran programming. I want to do a program in fortran where in the do loop it will read an input file from the first line upto a line and again it will go to the starting line of the file in the next loop and read it. It will continue reading in this process upto certain no. of loops. But I don't undestand how can I do this. I have tried to use "rewind" statement but it did not work. So could anyone please tell what should I do. Thanks in advance. :confused:
 
Technology news on Phys.org
fort_phys said:
Hi everybody,
I have just joined this forum and I have a problem in fortran programming. I want to do a program in fortran where in the do loop it will read an input file from the first line upto a line and again it will go to the starting line of the file in the next loop and read it. It will continue reading in this process upto certain no. of loops. But I don't undestand how can I do this. I have tried to use "rewind" statement but it did not work. So could anyone please tell what should I do. Thanks in advance. :confused:
Hi.
I'm no expert at all. Have you tried something like
Code:
do j=1, m
  do i=1, n
    read(1,*)x(i)
  end do
end do
This code will read a file of n rows -thus up to n lines-, m times. If I misunderstood feel free to give more info about your code so that it might help more experienced people than me.
 
It appears to me that you want to read the data again from the same file, from its beginning.

If the 'rewind' statement doesn't reset the file to its beginning, it may be because your operating system doesn't allow that operation. Try closing the file, then opening it again. For example, if your file is attached to unit 10:

Code:
      close (unit=10)
      open (unit=10, file='filename.txt', ...)
 
Thanks fluidistic and jtbell for your reply. I tried both of yours advices but it did not work. I am attaching my program here below. Please have a look.

open(unit=2,file="input.dat")
c = 0
80 i = 0
c = c + i*Nat
do x = 1, 7+At(1)+c
read(2,*)
enddo !x
! do i = 1, Nband
do j = 1, Nkp
do l = 1,IONS
read(2,*)y,s
write(3,*)j,y,s
enddo!i
do q = 1, 3027-IONS
read(2,*)
enddo!q
enddo!j
close (2)
i = i + 1
if (i == 112) go to 70
go to 80
70 end program

There is no problem in compiling the program. But during run time it is showing an error:

forrtl: No such file or directory
forrtl: severe (29): file not found, unit 2, file /matsc/students/user/generator/TRY/fort.2
Image PC Routine Line Source
a.out 080925C3 Unknown Unknown Unknown
a.out 08091BE3 Unknown Unknown Unknown
a.out 0806547E Unknown Unknown Unknown
a.out 0804BDCC Unknown Unknown Unknown
a.out 0804BA67 Unknown Unknown Unknown
a.out 080568C4 Unknown Unknown Unknown
a.out 0804A195 Unknown Unknown Unknown
a.out 08049B81 Unknown Unknown Unknown
libc.so.6 00B96390 Unknown Unknown Unknown
a.out 08049A91 Unknown Unknown Unknown

So can you please suggest anything else.Thanks.
 
That error message indicates that the program is trying to open the file 'fort.2' on unit 2. 'fort.2' is the default file name associated with unit 2, so my guess is that your "input.dat" is invalid because of using double quotes instead of single quotes. In the "open" statement, try putting the file name in single quotes: 'input.dat'
 
Ok. I'd love to see the whole code. May you please post it within
Code:
 tags?
As far of now I see the line "read(2,*)"
But you haven't assigned anything to read.  I don't know if that can cause an error but it's useless at best as it is.  It might even cause you the error you see.  
I also see something that's weird: i = i + 1 and then after a few lines the program ends.  It's not even inside a loop and I suspect it's meant to be inside a loop but I might be wrong.
Also I know that the line "70 end program" is wrong.  That's something I've learned recently on this forum (see [url]https://www.physicsforums.com/showthread.php?t=486627[/url]) so you might want to change it.
But I suggest to post your entire code :)
 
First, "rewind" should have worked, unless the file is something where rewind makes no sense (for example if you were typnig input at the terminal).

This should work IMO. Nnote, if you use "rewind", you don't use "close" as well:

enddo!j
i = i + 1
if (i == 112) go to 70
rewind(2)
go to 80
70 end program

Your code in post #2 is wrong, because after you closed the file you need to open it again. The second time around, the read(2,*) found that the unit 2 was closed, so it tried to open the default file name "fort.2" which did not exist.

This should also work, but "rewind" is more efficient than "close" and "open":

c = 0
80 open(unit=2,file="input.dat")
i = 0
... etc ...
enddo!j
close (2)
i = i + 1
if (i == 112) go to 70
go to 80
70 end program
 
jtbell said:
That error message indicates that the program is trying to open the file 'fort.2' on unit 2. 'fort.2' is the default file name associated with unit 2, so my guess is that your "input.dat" is invalid because of using double quotes instead of single quotes. In the "open" statement, try putting the file name in single quotes: 'input.dat'

I don't think this is the error. My gfortran does compile when I use a double quote.
AlephZero said:
70 end program
I don't think this will work. Remember this (https://www.physicsforums.com/showthread.php?t=486627) thread? :D
 
fort_phys said:
Hi everybody,
I have just joined this forum and I have a problem in fortran programming. I want to do a program in fortran where in the do loop it will read an input file from the first line upto a line and again it will go to the starting line of the file in the next loop and read it. It will continue reading in this process upto certain no. of loops. But I don't undestand how can I do this. I have tried to use "rewind" statement but it did not work. So could anyone please tell what should I do. Thanks in advance. :confused:

read(10, *) x
rewind 10
read(10, *) x

will read the first record of same file both times,
and the same value will be stored in x both times.
 
  • #10
fort_phys said:
Thanks fluidistic and jtbell for your reply. I tried both of yours advices but it did not work. I am attaching my program here below. Please have a look.

open(unit=2,file="input.dat")
c = 0
80 i = 0
c = c + i*Nat
do x = 1, 7+At(1)+c
read(2,*)
enddo !x
! do i = 1, Nband
do j = 1, Nkp
do l = 1,IONS
read(2,*)y,s
write(3,*)j,y,s
enddo!i
do q = 1, 3027-IONS
read(2,*)
enddo!q
enddo!j
close (2)
i = i + 1
if (i == 112) go to 70
go to 80
70 end program

There is no problem in compiling the program. But during run time it is showing an error:
...
So can you please suggest anything else.Thanks.

Don't use units 1 thru 7.
Better to use units 10 upwards.
Units 1 to 7 are used for keyboard and VDU input-output,
and some of those numbers are reserved.

Use REWIND instead of CLOSE.

CLOSE loses the connection to file INPUT.DAT
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K