I would like to use deferred-length character strings in a "simple" manner to read user input. The reason that I want to do this is that I do not want to have to declare the size of a character string before knowing how large the user input will be. I know that there are "complicated" ways to do this. For example, the iso_varying_string module can be used: https://www.fortran.com/iso_varying_string.f95. Also, there is a solution here: Fortran Character Input at Undefined Length. However, I was hoping for something as simple, or almost as simple, as the following:
program main
character(len = :), allocatable :: my_string
read(*, '(a)') my_string
write(*,'(a)') my_string
print *, allocated(my_string), len(my_string)
end program
When I run this program, the output is:
./a.out
here is the user input
F 32765
Notice that there is no output from write(*,'(a)') my_string
. Why?
Also, my_string
has not been allocated. Why?
Why isn't this a simple feature of Fortran? Do other languages have this simple feature? Am I lacking some basic understanding about this issue in general?
Problem Statement#1: Write a C program to read a single character as input in C. scanf () needs to know the memory location of a variable in order to store the input from the user. So, the ampersand will be used in front of the variable (here ch) to know the address of a variable.
Here using %c format specifier, the compiler can understand that character type of data is in a variable when taking input using the scanf () function Problem Statement#2: Write a C program to read words as input from the user. First, initialize the char array of size ( greater than are equal to the length of word).
Thus this can be used in limiting the user with some sensitive inputs like username, password, etc. We enter the argument -n followed by the number of characters we want to limit to. This is done to input from the user in a time-constrained way.
In the first example, we will use the Scanner class to take the input. We use scanner.next ().charAt (0) to read the input as char. charAt (0) reads read the first character from the scanner. The next example uses the System.in directly to call the read () method.
vincentjs's answer isn't quite right.
Modern (2003+) Fortran does allow automatic allocation and re-allocation of strings on assignment, so a sequence of statements such as this
character(len=:), allocatable :: string
...
string = 'Hello'
write(*,*)
string = 'my friend'
write(*,*)
string = 'Hello '//string
write(*,*)
is correct and will work as expected and write out 3 strings of different lengths. At least one compiler in widespread use, the Intel Fortran compiler, does not engage 2003 semantics by default so may raise an error on trying to compile this. Refer to the documentation for the setting to use Fortran 2003.
However, this feature is not available when reading a string so you have to resort to the tried and tested (aka old-fashioned if you prefer) approach of declaring a buffer of sufficient size for any input and of then assigning the allocatable variable. Like this:
character(len=long) :: buffer
character(len=:), allocatable :: string
...
read(*,*) buffer
string = trim(buffer)
No, I don't know why the language standard forbids automatic allocation on read
, just that it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With