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

AI Thread Summary
The discussion revolves around a Fortran programming issue where the user is trying to read an input file repeatedly within a loop. The user initially attempted to use the "rewind" statement to reset the file pointer but encountered issues. Suggestions from other forum members included using nested loops to read the file multiple times and ensuring the file is opened correctly. A key point raised was the need to use single quotes for file names in the "open" statement, as double quotes may lead to errors. Additionally, it was noted that closing and reopening the file could cause problems, and using "rewind" would be more efficient. The user also faced a runtime error indicating that the file could not be found, which was linked to the improper handling of file units. The importance of using higher unit numbers for file operations was emphasized to avoid conflicts with standard input/output units. Overall, the conversation highlighted common pitfalls in file handling in Fortran and provided troubleshooting advice.
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
Views
1K
Replies
12
Views
3K
Replies
2
Views
3K
Replies
5
Views
5K
Replies
8
Views
4K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Back
Top