replace_extension Function

public function replace_extension(fname, ext) result(res)

Changes the extension of a filename.

Syntax

 l = replace_extension(fname, ext)

Where:

  • fname: Name or path of the file.
  • ext: New extension for the name

It returns the fname value with the modified extension.

Example

The following program changes the extensions of some filenames.

 PROGRAM replace_extensionExample
    USE FU_Files, ONLY: replace_extension
    IMPLICIT NONE
    CHARACTER(LEN=:), ALLOCATABLE :: path
    path='file1.txt'
    WRITE(*,*) path, ' ', replace_extension(path, 'dat')
    path='./file1.txt'
    WRITE(*,*) path, ' ', replace_extension(path, 'dat')
    path='/tmp/file1.txt'
    WRITE(*,*) path, ' ', replace_extension(path, 'dat')
    path='/tmp/file1.dat'
    WRITE(*,*) path, ' ', replace_extension(path, '')
    path='/tmp/file1'
    WRITE(*,*) path, ' ', replace_extension(path, 'dat')
 END PROGRAM replace_extensionExample

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: fname

Filename or path to a file.

character(len=*), intent(in) :: ext

New extension for the fname.

Return Value character(len=:), ALLOCATABLE

The fname value with the modified extension.


Contents