Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ API-doc/
scratch.txt
*.dat
*.stream
.venv/
35 changes: 35 additions & 0 deletions doc/specs/stdlib_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,41 @@ Read a whole line from a formatted unit into a string variable
{!example/io/example_get_line.f90!}
```

## `input` — convenience prompt + read from standard input

### Status

Experimental / convenience

### Description

`input(prompt)` displays `prompt` (if provided) on the same line and returns the full user input as a string. Trailing spaces and tabs are preserved. Optionally the call can provide an `iostat`-like integer to capture the status.

### Syntax

`s = input(prompt [, iostat])`
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

Parameter name inconsistency: The documentation refers to the parameter as iostat but the implementation uses stat. These should match for clarity. Consider updating the documentation to use stat to match the implementation, or update the implementation to use iostat to match the documentation and align with standard Fortran conventions.

Suggested change
`s = input(prompt [, iostat])`
`s = input(prompt [, stat])`

Copilot uses AI. Check for mistakes.

### Arguments

`prompt`: Optional character input for the prompt message.
This argument is `intent(in)`.

`iostat`: Optional integer output for the status.
This argument is `intent(out)`.

### Return value

Deferred-length character string containing the input.

### Example

```fortran
use stdlib_io, only: input
character(len=:), allocatable :: name
integer :: iostat
name = input('Enter your name: ', iostat)
```

## Formatting constants

### Status
Expand Down
3 changes: 3 additions & 0 deletions example/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ ADD_EXAMPLE(loadtxt)
ADD_EXAMPLE(open)
ADD_EXAMPLE(savenpy)
ADD_EXAMPLE(savetxt)
# ADD_EXAMPLE(input) -- Interactive example, do not run as test
add_executable(example_input example_input.f90)
target_link_libraries(example_input "${PROJECT_NAME}")
21 changes: 21 additions & 0 deletions example/io/example_input.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
program example_input
use stdlib_io, only: input
implicit none
character(len=:), allocatable :: name
integer :: stat

print *, "Example of input() usage:"

! Simple usage
name = input("Enter your name: ")
print *, "Hello, ", name, "!"

! Usage with status check
name = input("Enter something else (or Ctrl+D to fail): ", stat)
if (stat == 0) then
print *, "You entered: ", name
else
print *, "Input failed or EOF encountered (stat=", stat, ")"
end if

end program example_input
Loading