Read Complete File to Array of Lines

H. S. Teoh hsteoh at quickfur.ath.cx
Fri May 11 08:19:16 PDT 2012


On Fri, May 11, 2012 at 05:00:16PM +0200, Paul wrote:
> I would like to read a complete file in one statement and then
> process it line by line.
> 
> foreach (line; MyFile)
> etc.
> 
> Is it possible to read a file into and array of lines?

import std.array;
import std.stdio;

string[] getLines(File f) {
	auto a = appender!string;
	foreach (line; f.byLine()) {
		a.put(line);
	}
	return a.data;
}

void main(string[] args) {
	if (args > 1) {
		auto f = File(args[0]);
		auto lines = getLines(f);
	}
}


T

-- 
"How are you doing?" "Doing what?"


More information about the Digitalmars-d-learn mailing list