system_remove Function

public impure elemental function system_remove(path) result(err)

NAME

  system_remove(3f) - [M_system_FILE_SYSTEM] call remove(3c) to remove file
  (LICENSE:PD)

SYNOPSIS

elemental impure function system_remove(path) result(err)

character(*),intent(in) :: path
integer(c_int)          :: err

DESCRIPTION

Fortran supports scratch files via the OPEN(3c) command; but does
not otherwise allow for removing files. The system_remove(3f) command
allows for removing files by name that the user has the authority to
remove by calling the C remove(3c) function.

EXAMPLE

Sample program:

program demo_system_remove
use M_system, only : system_remove
character(len=*),parameter :: FILE='MyJunkFile.txt'
integer :: ierr
write(*,*)'BEFORE CREATED '//FILE
call execute_command_line('ls -l '//FILE)
write(*,*)

! note intentionally causes error if file exists
open(unit=10,file=FILE,status='NEW')
write(*,*)'AFTER OPENED '//FILE
call execute_command_line('ls -l '//FILE)
write(*,*)

write(10,'(a)') 'This is a file I want to delete'
close(unit=10)
write(*,*)'AFTER CLOSED '
call execute_command_line('ls -l '//FILE)
write(*,*)

ierr=system_remove(FILE)
write(*,*)'AFTER REMOVED',IERR
call execute_command_line('ls -l '//FILE)
write(*,*)

end program demo_system_remove

Expected Results:

>  BEFORE CREATED MyJunkFile.txt
> ls: cannot access 'MyJunkFile.txt': No such file or directory
>
>  AFTER OPENED MyJunkFile.txt
> -rw-r--r-- 1 JSU None 0 Nov 19 19:32 MyJunkFile.txt
>
>  AFTER CLOSED
> -rw-r--r-- 1 JSU None 32 Nov 19 19:32 MyJunkFile.txt
>
>  AFTER REMOVED           0
> ls: cannot access 'MyJunkFile.txt': No such file or directory

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

Type IntentOptional Attributes Name
character, intent(in) :: path

Return Value integer(kind=c_int)


Contents

Source Code


Source Code

elemental impure function system_remove(path) result(err)

! ident_16="@(#) M_system system_remove(3f) call remove(3c) to remove file"

character(*),intent(in) :: path
integer(c_int)          :: err
character(kind=c_char,len=1),allocatable :: temp(:)

interface
   function c_remove(c_path) bind(c,name="remove") result(c_err)
      import c_char,c_int
      character(kind=c_char,len=1),intent(in) :: c_path(*)
      integer(c_int)                          :: c_err
   end function
end interface
!-----------------------------------------------------------------------------------------------------------------------------------
   temp = str2_carr(trim(path)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   err= c_remove(temp)
end function system_remove