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.


Relevant Pages

  • Re: what if (f)printf returns EINTR ?
    ... vsnprintf - formatted output conversion ... int fprintf; ... write to the character string str. ...
  • Proposal: String::Format::General
    ... It provides format string parsing and output assembly, you provide the code that implements the individual conversion characters. ... Format syntax is kind of a cross between sprintf and strftime, but how close it is to each of these depends on the semantics implemented by the user. ... Note that the following is pre-alpha documentation; the interface to the output conversion code has changed since yesterday, ... conversion character, and contain a number of optional fields which may ...
  • Re: New utf8string design may make UTF-8 the superior encoding
    ... A flag value of Zero is assigned to BytePerCodepoint ... likely to be four bytes, plus at least one byte for the character itself, ... stop focussing on such silly concepts as conversion time mattering in the ... that everyone can write scripts in their native language character set. ...
  • Re: question abt fscanf
    ... >I belive tht data in file is stored as ascii character constants i.e ... The scanf engine obeys the directives given in the format string. ... introduces a conversion, the 'l' modifier, and the 'd' conversion ... and convert a "long int" rather than a plain int, ...
  • Re: Character KIND conversion?
    ... One thing I noticed is the lack of a CHARACTER kind conversion function, even though there are such functions for all other intrinsic types, including LOGICAL. ... Which, of course, is only guaranteed to work for characters in the ASCII data set. ... Interestingly, the Fortran-95 ISO-VARYING-STRING standard does define one, for converting between the varying strings and normal fortran strings; it's an overload to the CHARfunction. ...