Printing Directory List in Fortran

How can I print a list of all files in a given directory using Fortran, restricting only certain types of files ... For example, I want to get only a list of .txt files from a directory.

Files in the directory:

  • file1.txt
  • another.dat
  • test.mp3
  • file2.txt
  • file3.txt

sort of

WHILE (not_last_file AND filetype = '.txt')

{

print FILENAME 

}

I am very grateful for your help,

thanks

+4
source share
1 answer

In a few words you cannot. There is no built-in library for such operations in Fortran that helps you. How you approach this problem will also depend on the version of Fortran that you are using (F77, F90, F95, etc.) that you did not mention.

"On a POSIX system using the latest 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 to be written to the directory.

See this post Listing the contents of a directory in Fortran or you can also see this review from the gfortran documentation useful. There is a lot of information about performing this operation with simple Google.

Hope this helps.

+3
source

All Articles