Remove all blank lines from a file

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 31 08:37:01 PDT 2017


On Thursday, 31 August 2017 at 14:44:07 UTC, vino wrote:
> Hi All,
>
>   Can some provide me a example of how to remove all blank 
> lines from a file.
>
> From,
> Vino.B

Super verbose, but:

import std.stdio;
import std.file;
import std.algorithm.iteration;

enum inFilename = "in.txt";
enum outFilename = "out.txt";

void main()
{
     immutable lines = readText(inFilename);

     char[] outbuffer;
     outbuffer.reserve(lines.length);

     foreach (line; lines.splitter("\n"))
     {
         if (!line.length) continue;

         outbuffer ~= line;
         outbuffer ~= "\n";
     }

     auto outfile = File(outFilename, "w");
     outfile.write(outbuffer);
}



More information about the Digitalmars-d-learn mailing list