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

  • Context: Fortran 
  • Thread starter Thread starter ivans1984
  • Start date Start date
  • Tags Tags
    files Fortran
Click For Summary
SUMMARY

The discussion focuses on using Fortran to open multiple text files based on a list of filenames stored in an array. The original code attempted to open files using the syntax 'file(i)', which resulted in the program looking for a file literally named 'file(i)'. The correct approach is to use the syntax without quotes: 'file(i)', allowing Fortran to interpret it as a variable containing the filename. The user confirmed that this correction resolved the issue.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with character arrays in Fortran
  • Knowledge of file handling in Fortran
  • Basic looping constructs in Fortran
NEXT STEPS
  • Review Fortran file I/O operations
  • Learn about character arrays and their manipulation in Fortran
  • Explore advanced looping techniques in Fortran
  • Investigate error handling in Fortran file operations
USEFUL FOR

This discussion is beneficial for Fortran developers, students learning file handling in Fortran, and anyone looking to efficiently manage multiple file operations in their code.

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!
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K