problems countered after while(read()){} terminated with ^D or EOF

Tyro[17] nospam at home.com
Thu Mar 22 12:12:33 PDT 2012


I'm using the following to read arrays from the command line (or 
redirected file) but am having some issues that I have not been 
able to solve on my own. Would appreciate if a little guidance.

void f(T)(ref T a)if(isArray!T)
{
     a.length = 100;
     int i;
     while(readf(" %s", &a[i++])) // #1
         if(i == a.length) a.length *= 2;
     a.length[--i];
}

issue #1
     how do it terminate the input (or clear the buffer) such that 
subsequent calls to readf are not ignored?

     int[] i;
     f(i); // no matter how many time I call readf() after this 
point it is all always ignored
     double d;
     readf(" %s", &d); // not called

issue #2
     how do i read a string[] such that whitespace (all or one of 
my choosing) delineate the string boundary?

     readf(" %s ", &data)  sort of does the trick for spaces but 
it leaves all newlines embedded in substrings.  Assuming that 
input is provided one string per line, readf(" %s\n", &data) 
works similar to #1 above, however if there is multiple words per 
line separated by spaces an exception is immediately thrown from 
std.format.

Thanks,
Andrew


More information about the Digitalmars-d-learn mailing list