Skip to content

Commit 5256fc0

Browse files
committed
add specs
1 parent 69eaa20 commit 5256fc0

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

doc/specs/stdlib_io.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,58 @@ Exceptions trigger an `error stop` unless the optional `err` argument is provide
301301
```fortran
302302
{!example/io/example_get_file.f90!}
303303
```
304+
305+
## `delete_file` - Delete a file
306+
307+
### Status
308+
309+
Experimental
310+
311+
### Description
312+
313+
This subroutine deletes a specified file from the filesystem. It ensures that the file exists and is not a directory before attempting deletion.
314+
If the file cannot be deleted due to permissions, being a directory, or other issues, an error is raised.
315+
The function provides an optional error-handling mechanism via the `state_type` class. If the `err` argument is not provided, exceptions will trigger an `error stop`.
316+
317+
### Syntax
318+
319+
`call [[stdlib_fs(module):delete_file(subroutine)]] (path [, err])`
320+
321+
### Class
322+
Subroutine
323+
324+
### Arguments
325+
326+
`path`: Shall be a character string containing the path to the file to be deleted. It is an `intent(in)` argument.
327+
328+
`err` (optional): Shall be a `type(state_type)` variable for error handling. If provided, errors are returned as a state object. If not provided, the program stops execution on error.
329+
330+
### Behavior
331+
332+
- Checks if the file exists. If not, an error is raised.
333+
- Ensures the path is not a directory before deletion.
334+
- Attempts to delete the file, raising an error if unsuccessful.
335+
336+
### Return values
337+
338+
The file is removed from the filesystem if the operation is successful. If the operation fails, an error is raised.
339+
340+
### Example
341+
342+
```fortran
343+
program example_delete_file
344+
use stdlib_fs
345+
implicit none
346+
347+
type(state_type) :: err
348+
349+
! Delete a file with error handling
350+
call delete_file("example.txt", err)
351+
352+
if (err%error()) then
353+
print *, "Failed to delete file:", err%print()
354+
else
355+
print *, "File deleted successfully."
356+
end if
357+
end program example_delete_file
358+
```

0 commit comments

Comments
 (0)