import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); // 配列型は行末まで読み込む assert(equal(c[], "ab")); // char型配列はトークンをそのまま返す assert(equal(d, "cde")); // stringもchar型配列と同様 assert(e == 1.0); // 小数も可 assert(equal(f, [1.0, 2.0])); assert(sc.read(a) == 0); // EOF
Scanner 速くはないが遅くもない printf/scanfよりちょっと遅いくらい?