March 22, 2012 problems countered after while(read()){} terminated with ^D or EOF | |
|---|---|
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
| |
March 22, 2012 Re: problems countered after while(read()){} terminated with ^D or EOF | |
|---|---|
Posted in reply to Tyro[17] | On 3/22/12, Tyro[17] <nospam@home.com> wrote:
> issue #2
> how do i read a string[] such that whitespace (all or one of
> my choosing) delineate the string boundary?
Jesse Phillips has a cmdln.interact library that I think would work by using:
string[] result = userInput!(string[])("Enter space-delimited values:");
But it seems he changed his nickname on github and hasn't reuploaded
the library yet.
In case you're ever reading from a file you can do:
string input = cast(string)std.file.read("filename");
string[] text = input.split();
|

Reply