Scanner

Scanner, not slow, not fast.

Constructors

this
this(File f)

f is source file

Members

Functions

hasNext
bool hasNext()

Do file has next token?

read
void read(auto ref Args args)

Read tokens. If can't read, throw exception.

unsafeRead
int unsafeRead(ref T x, auto ref Args args)

Read tokens. Return the number of success read

Examples

1 import std.path : buildPath;
2 import std.file : tempDir;
3 import std.algorithm : equal;
4 import std.stdio : File;
5 string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
6 auto fout = File(fileName, "w");
7 fout.writeln("1 2 3");
8 fout.writeln("ab cde");
9 fout.writeln("1.0 1.0 2.0");
10 fout.close;
11 Scanner sc = new Scanner(File(fileName, "r"));
12 int a;
13 int[2] b;
14 char[2] c;
15 string d;
16 double e;
17 double[] f;
18 sc.read(a, b, c, d, e, f);
19 assert(a == 1);
20 assert(equal(b[], [2, 3])); // array(except char) read whole line
21 assert(equal(c[], "ab")); // char array(char[], char[2], ...) return token
22 assert(equal(d, "cde")); // string is char array
23 assert(e == 1.0);
24 assert(equal(f, [1.0, 2.0]));
25 assert(!sc.hasNext);

Meta