Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions doc/specs/stdlib_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,52 @@ Read a whole line from a formatted unit into a string variable
```fortran
{!example/io/example_get_line.f90!}
```
## `input` — read a line from standard input

### Status

Experimental

### Description

Reads a line from standard input, optionally displaying a prompt.
This is similar to Python’s `input()` function.

The function returns the input as an allocatable character string.
Trailing spaces and tabs are preserved.
No numeric conversion is performed.

### Syntax

`line = ` [[stdlib_io(module):input(function)]] `([prompt][, iostat][, iomsg])`

### Arguments

`prompt` (optional):
A character expression containing a prompt to be displayed before reading input.

`iostat` (optional):
Default integer, contains status of reading from standard input.
Zero indicates success.

`iomsg` (optional):
Deferred-length character variable containing an error message if `iostat` is non-zero.

### Return value

Returns a deferred-length allocatable `character` variable containing the input line.

### Notes

- Trailing spaces and tabs are preserved
- No type conversion is performed
- To convert to numbers, use `read(line, *)`

### Example

```fortran
{!example/io/example_input.f90!}
```

## Formatting constants

Expand Down
2 changes: 2 additions & 0 deletions example/io/example_input.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = input("Enter your name: ")
print *, "Hello:", name
43 changes: 42 additions & 1 deletion src/stdlib_io.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module stdlib_io
implicit none
private
! Public API
public :: loadtxt, savetxt, open, get_line, get_file
public :: loadtxt, savetxt, open, get_line, get_file , input

!! version: experimental
!!
Expand Down Expand Up @@ -82,6 +82,13 @@ module stdlib_io
module procedure :: get_line_input_string
end interface get_line

!> Version: experimental
!>
!> Read a line from standard input with an optional prompt
interface input
module procedure :: input_char
end interface input

interface loadtxt
!! version: experimental
!!
Expand Down Expand Up @@ -597,6 +604,40 @@ contains
call get_line(input_unit, line, iostat, iomsg)
end subroutine get_line_input_char

!> Version: experimental
!>
!> Read a line from standard input with an optional prompt.
!! Similar to Python's input().
!!
!! - Preserves trailing whitespace
!! - Returns allocatable character string
!! - Does not perform type conversion
!! - Does not stop on error unless caller chooses to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clarified this to explicitly describe the behavior in terms of iostat/iomsg handling instead of using a vague statemen

function input_char(prompt, iostat, iomsg) result(line)
use, intrinsic :: iso_fortran_env, only : output_unit
character(len=*), intent(in), optional :: prompt
integer, intent(out), optional :: iostat
character(len=:), allocatable, optional :: iomsg
character(len=:), allocatable :: line

integer :: stat

! Print prompt without newline
if (present(prompt)) then
write(output_unit, '(a)', advance='no') prompt
end if

! Read line from stdin
call get_line_input_char(line, stat, iomsg)

if (present(iostat)) then
iostat = stat
else if (stat /= 0) then
call error_stop("input: error reading from standard input")
end if
end function input_char


!> Version: experimental
!>
!> Read a whole line from the standard input into a string variable
Expand Down
3 changes: 3 additions & 0 deletions test/io/test_input.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
call write_test_input(" abc ")
s = input()
call assert_equal(s, " abc ")