slurp

Reads a file line by line and parses the line into a single value or a std.typecons.Tuple of values depending on the length of Types. The lines are parsed using the specified format string. The format string is passed to std._format.formattedRead, and therefore must conform to the format string specification outlined in std._format.

Select!(Types.length == 1, Types[0][], Tuple!(Types)[])
slurp
(
Types...
)
(
string filename
,
scope const(char)[] format
)

Parameters

Types

the types that each of the elements in the line should be returned as

filename string

the name of the file to read

format const(char)[]

the format string to use when reading

Return Value

Type: Select!(Types.length == 1, Types[0][], Tuple!(Types)[])

If only one type is passed, then an array of that type. Otherwise, an array of std.typecons.Tuples.

Throws

Exception if the format string is malformed. Also, throws Exception if any of the lines in the file are not fully consumed by the call to std._format.formattedRead. Meaning that no empty lines or lines with extra characters are allowed.

Examples

import std.typecons : tuple;

scope(exit)
{
    assert(exists(deleteme));
    remove(deleteme);
}

write(deleteme, "12 12.25\n345 1.125"); // deleteme is the name of a temporary file

// Load file; each line is an int followed by comma, whitespace and a
// double.
auto a = slurp!(int, double)(deleteme, "%s %s");
assert(a.length == 2);
assert(a[0] == tuple(12, 12.25));
assert(a[1] == tuple(345, 1.125));

Meta