File.readf

Reads formatted data from the file using std._format.formattedRead.

  1. uint readf(Data data)
  2. uint readf(const(char)[] format, Data data)
    struct File
    uint
    readf
    (
    Data...
    )
    (
    scope const(char)[] format
    ,
    auto ref Data data
    )

Parameters

format const(char)[]

The format string. When passed as a compile-time argument, the string will be statically checked against the argument types passed.

data Data

Items to be read.

Return Value

Type: uint

Same as formattedRead: The number of variables filled. If the input range r ends early, this number will be less than the number of variables provided.

Examples

// test.d
void main()
{
    import std.stdio;
    auto f = File("input");
    foreach (_; 0 .. 3)
    {
        int a;
        f.readf!" %d"(a);
        writeln(++a);
    }
}
% echo "1 2 3" > input
% rdmd test.d
2
3
4
static import std.file;

auto deleteme = std.file.deleteme();
std.file.write(deleteme, "hello\nworld\ntrue\nfalse\n");
scope(exit) std.file.remove(deleteme);
string s;
auto f = File(deleteme);
f.readf!"%s\n"(s);
assert(s == "hello", "["~s~"]");
f.readf("%s\n", s);
assert(s == "world", "["~s~"]");

bool b1, b2;
f.readf("%s\n%s\n", b1, b2);
assert(b1 == true && b2 == false);

Meta