digitalmars.D.learn - formattedRead and whitespace-separated variables
- Joseph Rushton Wakeling (16/16) Apr 09 2013 Hello all,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/9) Apr 09 2013 Yes it is tolerant. A single space in the format strings reads and
- Joseph Rushton Wakeling (3/4) Apr 09 2013 I actually strip the leading/trailing whitespace from the line before th...
- Joseph Rushton Wakeling (2/4) Apr 09 2013 Forgot to say thank you! This is useful to know. :-)
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
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 whitespacebetweenvariables? (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
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
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