Skip to content

Commit e98deaa

Browse files
committed
add specs
1 parent 76292d3 commit e98deaa

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

doc/specs/stdlib_io.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,56 @@ Provides formats for all kinds as defined in the `stdlib_kinds` module.
260260
```fortran
261261
{!example/io/example_fmt_constants.f90!}
262262
```
263+
264+
## `getfile` - Read a whole ASCII file into a string variable
265+
266+
### Status
267+
268+
Experimental
269+
270+
### Description
271+
272+
This function reads the entirety of a specified ASCII file and returns its content as a string. 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`. The function also supports an optional flag to delete the file after reading.
273+
274+
### Syntax
275+
276+
`call [[stdlib_io(module):getfile(function)]] (fileName [, err] [, delete=.false.])`
277+
278+
### Class
279+
Function
280+
281+
### Arguments
282+
283+
`fileName`: Shall be a character input containing the path to the ASCII file to read. It is an `intent(in)` argument.
284+
285+
`err` (optional): Shall be a `type(state_type)` variable. It is an `intent(out)` argument used for error handling.
286+
287+
`delete` (optional): Shall be a `logical` flag. If `.true.`, the file is deleted after reading. Default is `.false.`. It is an `intent(in)` argument.
288+
289+
### Return values
290+
291+
The function returns a `string_type` variable containing the full content of the specified file.
292+
293+
Raises `STDLIB_IO_ERROR` if the file is not found, cannot be opened, read, or deleted.
294+
Exceptions trigger an `error stop` unless the optional `err` argument is provided.
295+
296+
### Example
297+
298+
```fortran
299+
program example_getfile
300+
use stdlib_io
301+
implicit none
302+
303+
type(string_type) :: fileContent
304+
type(state_type) :: err
305+
306+
! Read a file into a string
307+
fileContent = getfile("example.txt", err=err)
308+
309+
if (err%error()) then
310+
print *, "Error reading file:", err%print()
311+
else
312+
print *, "File content:", fileContent
313+
end if
314+
end program example_getfile
315+
```

0 commit comments

Comments
 (0)