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
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/
25 changes: 25 additions & 0 deletions doc/specs/stdlib_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,31 @@ 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])`

### Example

```fortran
use stdlib_io, only: input
character(len=:), allocatable :: name
integer :: st

name = input('Enter your name: ')
name = input('Enter name (with status): ', st)
```

## 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