View source code
Display the source code in std/stdio.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.stdio.File.readf

Reads formatted data from the file using formattedRead.

uint readf(alias format, Data...) (
  auto ref Data data
)
if (isSomeString!(typeof(format)));

uint readf(Data...) (
  in char[] format,
  auto ref Data data
);

Parameters

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

Example

// 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

Example

static import std.file;

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

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

Authors

Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen

License

Boost License 1.0.