Listing Directory Content in Fortran

How to get directory contents in Fortran 95?

-2
source share
4 answers

shure, if we have all the files in the "inFiles" folder, we first find out how many there are, and then we read their names in an array, check this:

real :: r integer :: i,reason,NstationFiles,iStation character(LEN=100), dimension(:), allocatable :: stationFileNames ! get the files call system('ls ./inFiles > fileContents.txt') open(31,FILE='fileContents.txt',action="read") !how many i = 0 do read(31,FMT='(a)',iostat=reason) r if (reason/=0) EXIT i = i+1 end do NstationFiles = i write(verb,'(a,I0)') "Number of station files: " , NstationFiles allocate(stationFileNames(NstationFiles)) rewind(31) do i = 1,NstationFiles read(31,'(a)') stationFileNames(i) 

! write (verb, '(a)') trim (stationFileNames (i)) end close (31)

+6
source

To be pedantic, you do not. There is no built-in or such Fortran 95 that will help you.

On a POSIX system and the recent Fortran compiler, you can use ISO_C_BINDING to create the POSIX opendir () and readdir () (or readdir_r () if you need thread safety) interfaces that allow iteration over a directory entry.

+3
source

Fortran has no concept of a directory. He is reading files. (There are some processors that do not even have a directory concept).

With that said, the easiest way would be with SYSTEM. Depends on what you want with this after ...

+1
source

You may find this review from the gfortran documentation. There are two libraries that attempt to provide access to POSIX functions and a wider collection of flibs.

0
source

All Articles