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

[fortran] open many files

  1. May 18, 2012 #1
    Hello oeveryone

    i've got many txt files each with a different name, such as

    filename1.txt
    filename2.txt
    ...


    i've got another txt in which i've got the list of the name
    of the files

    i want to allocate in a vector of characters che name of the files
    and use this to open all files with a do cycle, like

    do i=1,n

    open (10+i, file='file(i)',status='old')

    end do


    but doing so the code doesn't work, the file is not found
    (even though i successfully allocated filenames into the vector file(i))

    can you tell me where is the mistake?

    thanks
     
  2. jcsd
  3. May 18, 2012 #2

    jtbell

    User Avatar

    Staff: Mentor

    This tries to open a file named, literally, 'file(i)' (including the parentheses and the letter 'i'). Try instead:

    Code (Text):
    open (10+i, file=file(i),status='old')
    without the quotes, which uses the character string that is stored in file(i).

    (I assume you have declared 'file' as something like

    Code (Text):

    character*10 file(20)
     
    which is an array of 20 character strings, each with length 10.)
     
  4. May 19, 2012 #3
    thanks

    it works!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook