std.stdio.file.readln
- multiple declarations
- Function File.readln
- Function File.readln
Function File.readln
Read line from the file handle and write
it to
, including
terminating character.
buf
[]
This can be faster than line =
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.
File.readln
()
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)));
Parameters
Name | Description |
---|---|
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). |
Returns
0 for end of file, otherwise number of characters read
Throws
on I/O StdioException
error
, or UnicodeException
on Unicode
conversion error
.
Example
// Read lines fromstdin
into a string // Ignore lines starting with '#' // Write the string tostdout
void main() { string output; char[] buf; while (stdin.readln(buf)) { if (buf[0] == '#') continue; output ~= buf; } write(output); }
This method can be more efficient than the one in the previous example
because stdin.readln(
reuses (if possible) memory allocated
for buf
)
, whereas buf
line = stdin.readln()
makes a new memory allocation
for every line.
Function File.readln
Read line from the file handle and return it as a specified type.
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
version, which may offer
better performance as it can reuse its read buffer.
File.readln
(buf)
Prototype
S readln(S)( dchar terminator = '\x0a' ) if (isSomeString!S);
Parameters
Name | Description |
---|---|
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.
Returns
The line that was read, including the line terminator
character.
Throws
on I/O StdioException
error
, or UnicodeException
on Unicode conversion error
.
Example
// Readsstdin
and writes it tostdout
. import std.stdio; void main() { string line; while ((line = stdin.readln()) !is null) write(line); }
Authors
Walter Bright, Andrei Alexandrescu, Alex Rønne Petersen