View source code Display the source code in std/stdio.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

std.stdio.readln - multiple declarations

Function readln

Read line from stdin.

This version manages its own read buffer, which means one memory allocation per call. If you are not retaining a reference to the read data, consider the readln(buf) version, which may offer better performance as it can reuse its read buffer.

Prototype

S readln(S)(
  dchar terminator = '\x0a'
)
if (isSomeString!S);

Returns

The line that was read, including the line terminator character.

Parameters

NameDescription
S Template parameter; the type of the allocated buffer, and the type returned. Defaults to string.
terminator Line terminator (by default, '\n').

Note

String terminators are not supported due to ambiguity with readln(buf) below.

Throws

StdioException on I/O error, or UnicodeException on Unicode conversion error.

Example

Reads stdin and writes it to stdout.

import std.stdio;

void main()
{
    string line;
    while ((line = readln()) !is null)
        write(line);
}

Function readln

Read line from stdin and write it to buf[], including terminating character.

This can be faster than line = readln() because you can reuse the buffer for each call. Note that reusing the buffer means that you must copy the previous contents if you wish to retain them.

Prototypes

size_t readln(C)(
  C[] buf,
  dchar terminator = '\x0a'
)
if (isSomeChar!C && is(Unqual!C == C) && !is(C == enum));

size_t readln(C, R)(
  C[] buf,
  R terminator
)
if (isSomeChar!C && is(Unqual!C == C) && !is(C == enum) && isBidirectionalRange!R && is(typeof(terminator.front == (dchar).init)));

Returns

size_t 0 for end of file, otherwise number of characters read

Parameters

NameDescription
buf Buffer used to store the resulting line data. buf is resized as necessary.
terminator Line terminator (by default, '\n'). Use std.ascii.newline for portability (unless the file was opened in text mode).

Throws

StdioException on I/O error, or UnicodeException on Unicode conversion error.

Example

Reads stdin and writes it to stdout.

import std.stdio;

void main()
{
    char[] buf;
    while (readln(buf))
        write(buf);
}

Authors

Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen

License

Boost License 1.0.

Comments