Reputation: 33
My problem is that I want to inquire for an unknown filename in Fortran by using the extension of the file.
Example: a folder has a file with an extension "*.cel", the extension is always the same but the filename can be different because is given by the end-users. I want to detect whether any *.cel exists in the folder and get the name of the file.
Upvotes: 1
Views: 1616
Reputation: 8140
This kind of file system interaction is a bit hard. You'd think that someone would have created a module for that, but there are precious few, and I haven't come across one that was easy to use.
What most people do is either use a specific call to the system
or execute_command_line
subroutines to let the shell do the work, see here:
program my_ls
use iso_fortran_env
implicit none
character(len=*), parameter :: ls_file = '/tmp/my_ls.tmp'
integer :: u, ios
character(len=30) :: filename
call execute_command_line('ls -1 > '//ls_file, wait=.TRUE., exitstat=ios)
if (ios /= 0) stop "Unable to get listing"
open(newunit=u, file=ls_file, iostat=ios, status="old", action="read")
if ( ios /= 0 ) stop "Error opening listing file "
do
read(u, *, iostat=ios) filename
if (is_iostat_end(ios)) exit
if (ios /= 0) STOP "Unexpected error while reading listing file"
if (index(filename, ".cel") > 0) then
print*, filename
end if
end do
close(u)
call execute_command_line('rm '//ls_file, wait=.FALSE.)
end program my_ls
But what would be easier is to give the filenames as a command line argument when starting the program:
program my_ls2
use iso_fortran_env
implicit none
integer :: num_files, i
character(len=64), allocatable :: filenames(:)
num_files = command_argument_count()
allocate(filenames(num_files))
do i = 1, num_files
call get_command_argument(i, filenames(i))
end do
write(*, '(A)') filenames
end program my_ls2
Calling this program with
$ my_ls2 *.cel
will give you every file you want.
Upvotes: 2
Reputation: 7452
Simple answer: I'm afraid you can't do that in standard Fortran. You're going to have to look at your compiler documentation to find if it supports an extension through which you can implement what you require.
Upvotes: 1