Read a file line by line: Difference between revisions

Content added Content deleted
m (Link to input loop task)
(Added D version)
Line 4: Line 4:
See also: [[Input loop]].
See also: [[Input loop]].


=={{header|D}}==
<lang d>import std.stdio;

void main() {
foreach (line; File("foobar.txt").byLine())
write(line);
}</lang>
The File is managed by reference count, and it gets closed when it gets out of scope or it changes. The 'line' is a char[] (with newline), so if you need a string you have to idup it.
=={{header|Python}}==
=={{header|Python}}==
For the simple case of iterating over the lines of a file you can do:
For the simple case of iterating over the lines of a file you can do: