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

In summary, The speaker has multiple text files with different names and wants to open them using a do cycle. They have a separate text file containing the list of file names and have allocated them to a character vector. However, the code is not working and the speaker asks for help in finding the mistake. The summarizer suggests removing the quotes and using the character string stored in the vector, which the speaker confirms works.
  • #1
ivans1984
2
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
  • #2
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.)
 
  • #3
thanks

it works!
 

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

1. Can I open multiple files simultaneously in Fortran?

Yes, Fortran allows you to open multiple files at once using the "OPEN" statement. You can specify the names of the files and their respective file numbers in the statement, and they will all be opened at the same time.

2. What is the maximum number of files that can be opened in Fortran?

The maximum number of files that can be opened in Fortran depends on the specific compiler and operating system being used. However, it is typically limited to a few hundred files.

3. How can I check if a file has been successfully opened in Fortran?

You can use the "STATUS" specifier in the "OPEN" statement to check if a file has been successfully opened. If the file is opened successfully, the "STATUS" value will be zero. If there is an error, the "STATUS" value will be a non-zero number indicating the type of error.

4. Is it possible to open files in Fortran for both reading and writing?

Yes, the "OPEN" statement allows you to specify the file mode using the "ACCESS" specifier. You can use "ACCESS='APPEND'" to open a file for writing, and "ACCESS='SEQUENTIAL'" to open a file for both reading and writing.

5. How can I close multiple files at once in Fortran?

To close multiple files at once in Fortran, you can use a "CLOSE" statement with the "UNIT" specifier followed by a list of file numbers separated by commas. This will close all the specified files simultaneously.

Similar threads

  • Programming and Computer Science
Replies
20
Views
583
  • Programming and Computer Science
Replies
1
Views
610
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top