reading formatted strings: readf("%s", &stringvar)

Tyro[17] nospam at home.com
Mon Mar 26 13:52:35 PDT 2012


On Monday, 26 March 2012 at 14:41:41 UTC, Andrei Alexandrescu 
wrote:
> On 3/26/12 5:55 AM, Tyro[17] wrote:
>> You can achieve the same with:
>>
>> readf(" %s\n", &s2);
>>
>> My goal however, is not to read one line of information. 
>> Rather, it is to
>> read multiple lines of information from standard input. I get 
>> close to
>> being able to do so if i don't including "\n" as a part of my 
>> format string
>> or if I changing your suggestion to
>>
>> while (!stdin.eol()) {
>> s2 = chomp(readln());
>> }
>>
>> but again I run into the predicament was before, a need to 
>> close the
>> the stream with Ctrl-D/Ctrl-Z.
>
> I made the decision for the current behavior while implementing 
> readf. Basically I tried to avoid what I think was a mistake of 
> scanf, i.e. that of stopping string reading at the first 
> whitespace character, which is fairly useless.

Couldn't the state of stdin be checked upon entrance into readf
and reopened if it is already closed?

Wouldn't that accomplish the desired effect while avoiding
the pitfalls of scanf?

> Over the years scanf was improved with %[...] which allows 
> reading strings with any characters in a set.
>
> Anyway, if I understand correctly, there's no way to achieve 
> what you want unless you read character-by-character and define 
> your own control character. There's no out-of-band character 
> that means "end of this input, but not that of the file".
>
>
> Andrei



More information about the Digitalmars-d mailing list