Re: Most elegant way to read to allocatable array?

yair999@xxxxxxxxx wrote:
Richard Maine wrote:

Yes, I saw the emphasis on the "might" part. To elaborate on it, the use of a
plain "i" (without a field width) is nonstandard. Some compilers accept it.

Well, my Intel Visual Fortran 9.1 accepted it during compilation, but
running it yielded:

forrtl: severe (64): input conversion error, unit 20, file C:\kuku.txt

OK. You might be able to do this:

Initialize n (the element count) to 0.

Read a character without advancing. If the character is a decimal digit, multiply n by 10, add the numeric value of the digit you just read, and do this again.

If the character is a space, allocate an array of size n, read it free form, and you're done, unless you have bad input data somewhere.

If the character is not a space, you have bad input data right here.

But you wanted elegance...

You might be able to hide the alpha to binary conversion in a function to which you could pass the logical unit number of your input file:

n = read_int_with_no_space(20) [I don't know if underscores are OK]
read(20, *) A

To my knowledge, converting a decimal digit character ('0' through '9') to its numeric value (0 through 9) is relatively simple:

ichar(c) - ichar('0')

This should work with both ASCII and EBCDIC.