Thread overview
Formatted read of tokens?
Jun 17, 2014
Jerry
Jun 17, 2014
bearophile
Jun 17, 2014
Jerry
Jun 17, 2014
John Colvin
Jun 18, 2014
Jerry
June 17, 2014
Hi all,

I'm porting some C++ code into D that uses istream to read lines like

label 3

where there can spaces or tabs between the 2 fields.  In c++, this is:

string l;
int i;
istr >> l >> i;

What's the equivalent in D?  It appears that D formatted read for strings grabs everything up to a newline and doesn't support whitespace tokenization.

If I do

f.readf("%s %s", &l, &i);

it fails if the whitespace is a tab.

Thanks
Jerry
June 17, 2014
Jerry:

> If I do
>
> f.readf("%s %s", &l, &i);
>
> it fails if the whitespace is a tab.

In you can use byLine, followed by a split, and then assignment of the pieces, followed by to!int where necessary.

Bye,
bearophile
June 17, 2014
"bearophile" <bearophileHUGS@lycos.com> writes:

> Jerry:
>
>> If I do
>>
>> f.readf("%s %s", &l, &i);
>>
>> it fails if the whitespace is a tab.
>
> In you can use byLine, followed by a split, and then assignment of the pieces, followed by to!int where necessary.

I actually can't use byLine in this instance.  I'm really wanting the %s
behavior of scanf().
June 17, 2014
On Tuesday, 17 June 2014 at 13:16:38 UTC, Jerry wrote:
> "bearophile" <bearophileHUGS@lycos.com> writes:
>
>> Jerry:
>>
>>> If I do
>>>
>>> f.readf("%s %s", &l, &i);
>>>
>>> it fails if the whitespace is a tab.
>>
>> In you can use byLine, followed by a split, and then assignment of the pieces,
>> followed by to!int where necessary.
>
> I actually can't use byLine in this instance.  I'm really wanting the %s
> behavior of scanf().

If you really need/want fscanf behaviour then just use it:

import core.stdc.stdio : fscanf;
June 18, 2014
"John Colvin" <john.loughran.colvin@gmail.com> writes:

> On Tuesday, 17 June 2014 at 13:16:38 UTC, Jerry wrote:
>> "bearophile" <bearophileHUGS@lycos.com> writes:
>>
>>> Jerry:
>>>
>>>> If I do
>>>>
>>>> f.readf("%s %s", &l, &i);
>>>>
>>>> it fails if the whitespace is a tab.
>>>
>>> In you can use byLine, followed by a split, and then assignment of the
>>> pieces,
>>> followed by to!int where necessary.
>>
>> I actually can't use byLine in this instance.  I'm really wanting the %s
>> behavior of scanf().
>
> If you really need/want fscanf behaviour then just use it:
>
> import core.stdc.stdio : fscanf;

Thanks, I forgot the obvious.

It would be nice to have a way to do the same thing in D formatted reading.  Does it seem like it would be useful to specify this kind of thing?

For example, you could have:

%' '         grabs all isWhite() chars
%t           reads a single token - all chars !isWhite()

Any takers?

Jerry