www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - formattedRead and whitespace-separated variables

reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
Hello all,

A bit of a naive question here as text processing isn't my forte ...

Suppose I have a data file containing whitespace-separated variables, i.e.

    1 2 3
    4 5 6
    7 8 9
    10 11 12

...etc.

Naively it's easy to do something like (having got a stripped string of the
line),

    formattedRead(s, "%s %s %s", &var1, &var2, &var3);

... but is this tolerant of arbitrary differences in the whitespace between
variables?  (e.g. it might be 2 spaces not 1; or a tab; or ...)

I don't think it is, so how would I tweak the formatting string so as to ensure
that any arbitrary whitespace is taken as separating the variables to read?

Thanks & best wishes,

    -- Joe
Apr 09 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/09/2013 04:42 AM, Joseph Rushton Wakeling wrote:> Hello all,

      formattedRead(s, "%s %s %s", &var1, &var2, &var3);

 ... but is this tolerant of arbitrary differences in the whitespace 
between
 variables?  (e.g. it might be 2 spaces not 1; or a tab; or ...)
Yes it is tolerant. A single space in the format strings reads and ignores all white space at that spot. I suggest you put a space before the first %s as well. Ali
Apr 09 2013
next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 04/09/2013 07:09 PM, Ali Çehreli wrote:
 I suggest you put a space before the first %s as well.
I actually strip the leading/trailing whitespace from the line before this point, so it should be unnecessary.
Apr 09 2013
prev sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 04/09/2013 07:09 PM, Ali Çehreli wrote:
 Yes it is tolerant. A single space in the format strings reads and ignores all
 white space at that spot.
Forgot to say thank you! This is useful to know. :-)
Apr 09 2013