Fortran90: read file with variable name ( character)

In summary, the conversation discusses how to create a Fortran90 program that can open files with a specific naming pattern. The program will need to read the first four digits as a variable character, followed by an increasing integer value, and ending with ".dat". It is suggested to use a loop to accomplish this task, with an example provided in the code.
  • #1
typeon
4
0
Hi there,

I am trying to make a Fortran90 program that open files with this pattern:

1a041.dat
wp2d2.dat
qad33.dat
mntf4.dat
erh55.dat
...etc.
so the first four digits I want to consider them as a variable character, then it comes an integer value that increases by one for every file. The last part is the ".dat"

I don't know how to tell my program to read this character for every file and then to open it.

I think I have managed to sort out the integer part by converting the integer value into a string value by using write(numchr,*) j.

This is an extract of the program:
PROGRAM database
character(40) :: numchr ! this is to turn integer into string
character(10) :: prot ! this is to read the first 4 digits
integer :: j,n,i,k

n = 10
DO j = 1,n
write(numchr,*) j

open (j, file = prot // trim(adjustl(numchr)) // ".dat",status='old')
etc...
close(j)
ENDDO

Many thanks!
 
Physics news on Phys.org
  • #2
The best way to do this is to use a loop in your code. Here is an example of how you can do it:PROGRAM databasecharacter(40) :: numchr ! this is to turn integer into stringcharacter(10) :: prot ! this is to read the first 4 digitsinteger :: j,n,i,kn = 10DO j = 1,nwrite(numchr,*) j open (j, file = prot // trim(adjustl(numchr)) // ".dat",status='old') FOR i = 1, 4 READ prot(i:i), numchr(i:i) END FOR ...etc... close(j)ENDDO
 

FAQ: Fortran90: read file with variable name ( character)

What is Fortran90?

Fortran90 is a high-level programming language commonly used for scientific and engineering applications. It was first introduced in the 1990s and is still widely used today.

How do I read a file with variable name in Fortran90?

To read a file with a variable name in Fortran90, you can use the READ command followed by the name of the file and the variable name. For example, READ(file, var) will read the contents of the file into the variable var.

Can I use character variables in Fortran90 to read files?

Yes, Fortran90 allows for the use of character variables to read files. You can use the CHARACTER data type to declare a variable and then use it to read the contents of a file.

What happens if the file name in Fortran90 is longer than the variable name?

In Fortran90, the file name can be longer than the variable name. However, if the file name is longer than the maximum allowed length for a variable name, it will be truncated to fit the maximum length. This may result in a loss of information from the file name.

Can I use a loop to read multiple files with variable names in Fortran90?

Yes, you can use a loop to read multiple files with variable names in Fortran90. By using a loop, you can easily read in multiple files with different variable names without having to write separate code for each file.

Similar threads

Replies
3
Views
4K
Replies
2
Views
6K
Replies
16
Views
3K
Replies
11
Views
6K
Replies
15
Views
2K
Back
Top