operating-systemfortranenvironment-variablespath-separator

Get file separator in Fortran


Could you please tell me how to get the file separator of current operating system, for example \ in Windows and / in Unix, in Fortran at run-time.


Solution

  • You can use Fortran 2003 Standard intrinsic procedure GET_ENVIRONMENT_VARIABLE to do something like this. Example:

    CHARACTER(LEN=99999) :: path
    CHARACTER(LEN=1)     :: path_separator
    CALL GET_ENVIRONMENT_VARIABLE('PATH',path)
    path_separator=path(1:1)
    WRITE(*,*)'Path separator is ',path_separator
    END
    

    This program will output "/" as a path separator in UNIX or Linux. You could get this from other environment variables as well. Notice that this example is hardwired for UNIX/Linux. You would need a bit different logic to extract e.g. "\" for Windows, but I am not familiar with this system. I vaguely remember from Win95 having something like "c:\.....", so it is likely that in case of Windows you would look for "\" in path(3:3).

    Hope this helps.