Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 21, 2011 #1
    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:
     
  2. jcsd
  3. Jun 21, 2011 #2

    fluidistic

    User Avatar
    Gold Member

    Hi.
    I'm no expert at all. Have you tried something like
    Code (Text):
    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.
     
  4. Jun 21, 2011 #3

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

          close (unit=10)
          open (unit=10, file='filename.txt', ...)
     
     
  5. Jun 22, 2011 #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.
     
  6. Jun 22, 2011 #5

    jtbell

    User Avatar

    Staff: Mentor

    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'
     
  7. Jun 22, 2011 #6

    fluidistic

    User Avatar
    Gold Member

    Ok. I'd love to see the whole code. May you please post it within
    Code (Text):
     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 :)
     
  8. Jun 22, 2011 #7

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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
     
  9. Jun 25, 2011 #8

    fluidistic

    User Avatar
    Gold Member

    I don't think this is the error. My gfortran does compile when I use a double quote.
    I don't think this will work. Remember this (https://www.physicsforums.com/showthread.php?t=486627) thread? :D
     
  10. Aug 13, 2011 #9
    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.
     
  11. Aug 13, 2011 #10
    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Reading a file repeatedly in each iteration of a do loop in fortran
  1. Read file in fortran (Replies: 2)

Loading...