[your code here]

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 29 21:48:11 UTC 2019


On Tue, Jan 29, 2019 at 09:13:16PM +0000, qjnr via Digitalmars-d wrote:
> /* This is a very basic implementation of cat in the D programming language.
>    For more info about the D programming language, visit https://dlang.org .
> 
>    Copyright (c) 2018 qjnr */
> 
> import std.stdio : readln, write, writef;
> import std.file : readText, exists;
> 
> void main(string[] args)
> {
>   if (args.length is 2)
                   ^^^^
That's a syntax error.


>   {
>     string fname = args[1];
>     if (!exists(fname))
>     {
>       writef("cat: %s: No such file or directory", fname);
>     }
>     else
>     {
>       string content = readText(fname);
>       write(content);
>     }
>   }
>   else
>   {
>     string line;
>     while ((line = readln()) !is null)
>     {
>       write(line);
>     }
>   }
> }

Mmm, too verbose, has syntax errors, doesn't handle multiple files, and
inefficient.

This is better:

	import std.algorithm : copy;
	import std.stdio;

	void main(string[] args)
	{
		auto w = stdout.lockingBinaryWriter;
		if (args.length > 1)
			foreach (f; args[1 .. $])
			{
				File(f, "r").byChunk(4096).copy(w);
			}
		else
			stdin.byChunk(4096).copy(w);
	}


T

-- 
The computer is only a tool. Unfortunately, so is the user. -- Armaphine, K5


More information about the Digitalmars-d mailing list