Fortran Can I Use Fortran to Open Multiple Files Using a List of File Names?

  • Thread starter Thread starter ivans1984
  • Start date Start date
  • Tags Tags
    files Fortran
AI Thread Summary
The discussion centers on a programming issue related to opening multiple text files using a loop. The user initially attempts to open files by referencing them as 'file(i)' within quotes, which leads to an error because it treats the string literally rather than as a variable. The correct approach is to remove the quotes, allowing the code to access the actual filename stored in the array 'file(i)'. It is also noted that the 'file' array should be properly declared, such as with 'character*10 file(20)', to ensure it can hold the filenames correctly. The solution provided resolves the user's issue successfully.
ivans1984
Messages
2
Reaction score
0
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
 
Technology news on Phys.org
ivans1984 said:
open (10+i, file='file(i)',status='old')

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

Code:
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:
character*10 file(20)

which is an array of 20 character strings, each with length 10.)
 
thanks

it works!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
5
Views
5K
Replies
41
Views
5K
Replies
16
Views
4K
Back
Top