Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

read from file

Moderators: larsivi kris

Posted: 09/24/07 23:10:11 Modified: 09/25/07 10:13:48


I want to read whitespace-separated strings from a file (I do not want to read the whole file into memory, the file might be quite big.). A fairly easy job one could believe, but I do not manage to get it working.

It would be great if someone could give me a short example how to do this.


Author Message

Posted: 09/25/07 00:18:25

Here's one that splits on lines:

The notion is similar, but you'd use one of the other Iterators provided. BTW, had you considered using virtual-files (memory-mapped files) for those cases where the file is enormous?

Posted: 09/25/07 09:15:28

Thanks for your answer!

I already had a look at the different stream iterators, the API documentation lists:

I can guess the function of LineIterator? and more or less of RegexIterator? (would be overkill here anyway); StreamIterator? seems to be a common base class for the other ones.

But I have no clue how QuoteIterator? and SimpleIterator? work. The documentation pages and look almost the same, and give no specific information.

Which Iterator is the right one for my purpose?

Posted: 09/25/07 10:34:05

QuoteIterator? is much the same as SimpleIterator?, with the only difference being that it will ignore delimiters between quotes. If you don't need that, use SimpleIterator? with the whitespace characters you want to split on passed as delims, ie auto delims = " \t\n"; or similar.

Posted: 09/26/07 01:05:12

Thanks for the explanation,

it works for me now.

Finally my problem came down to a mistake in the documentation

I guess that the line # this(T[] delim, InputStream? stream = null);

should read # this(InputStream? stream, T[] delim);

Maybe someone could correct that.

Posted: 09/26/07 07:44:10

mki wrote:

Are you sure? The stream iterator docs are certainly not good, but the function signatures are automatically created from the source via DDoc and is as far as I can see correct, also in that case.

Posted: 09/26/07 08:40:30

This works for me: auto file = new FileConduit?(fileName); auto iter = new SimpleIterator?!(char)(file,"\n\t ");

While this: auto iter = new SimpleIterator?!(char)("\n\t ", file);

gives: constructor!(char).SimpleIterator?.this (char[]) does not match parameter types (char[3u],FileConduit?) Error: cannot implicitly convert expression ("\x0a\x09 ") of type char[3u] to Error: cannot implicitly convert expression (file) of type to char[]

I use tango 0.98 (newest version in Gentoo portage D overlay). Is it possible that the order of the parameters to SimpleIterator?.this(...) has changed since this version?

Posted: 09/26/07 10:06:49

Ah, yes, I think that was changed in 0.99. Tango currently has 0.99.1 as it's latest release. I haven't seen the Gentoo guys around for a while, not sure how things are moving along there.

Posted: 09/26/07 19:57:24 -- Modified: 09/26/07 19:58:33 by

Ok, this explains a lot. I will ask the Gentoo people for an update.

I dare to ask another newbie question:

I can use my SimpleIterator? 'iter' to write a foreach loop over all whitespace-separated tokens in the input file. But this is quite restrictive, I want to read them one by some using a function which returns the next token. Something like this:

char[] nextToken =;

Is it possible to do this somehow?

Posted: 09/26/07 21:18:56

Have you tried that code? It should work :)

Posted: 09/26/07 21:53:32

Now I have tried my code... :)

If only more problems would dissapear as fast as this one...