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

In summary, The conversation discusses a problem in fortran programming where the program must read an input file in a do loop, starting from the first line up to a certain line, and then repeating the process up to a certain number of loops. The person has attempted to use the "rewind" statement, but it did not work. Other forum members suggest using a do loop within a do loop, or closing and reopening the file, but the program still shows an error during runtime.
  • #1
fort_phys
3
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
  • #2
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.
 
  • #3
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', ...)
 
  • #4
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.
 
  • #5
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'
 
  • #6
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 :)
 
  • #7
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
 
  • #8
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
 
  • #9
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
 

1. How do I read a file repeatedly in each iteration of a do loop in Fortran?

To read a file repeatedly in each iteration of a do loop in Fortran, you can use the READ statement inside the do loop. This statement reads the next line of the file on each iteration until the end of the file is reached.

2. Can I use a do loop to read a file multiple times in Fortran?

Yes, you can use a do loop to read a file multiple times in Fortran. Simply place the READ statement inside the do loop and use a loop index variable to control the number of times the file is read.

3. How do I exit the do loop when the end of the file is reached?

You can use the END= label in the do loop to check for the end of the file. If the end of the file is reached, the loop will terminate and control will move to the statement following the do loop.

4. What happens if I try to read past the end of the file in Fortran?

If you try to read past the end of the file in Fortran, an end-of-file (EOF) condition will occur. This means that the READ statement will not be able to read any more data and the program will continue execution without reading anything from the file.

5. Is it necessary to close the file after reading it in each iteration of the do loop?

Yes, it is necessary to close the file after reading it in each iteration of the do loop. This ensures that the file is properly closed and resources are released. Failure to close the file can result in memory leaks and other issues.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
7K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
6
Views
965
Back
Top