Record separator is being lost after string cast

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 4 03:39:18 PST 2015


On Wed, 04 Feb 2015 09:28:27 +0000, Kadir Erdem Demir wrote:

> I am sorry make a busy community more busy with false alarms.

don't mind it. ;-) "D.learn" is for *any* questions about language, no 
matter how strange they may seem.

> How can I write the code below better? How can I reduce the number of
> foreach? statements.

actually, your loop seems to be not good anyway, as it may easily read 
only part of a line. sadly, there is no streaming interface to gz files, 
so your best bet is to read the whole file in memory, then unpack it all 
at once, and then process it. just be sure that you have enough RAM. 
something like this:

  import std.stdio;
  import std.string;
  import std.zlib;

  void main () {
    char[] unpacked;
    // read the whole file and unpack it
    {
      auto fl = File("test.txt.gz", "rb");
      auto packed = new ubyte[](cast(usize)fl.size);
      fl.rawRead(packed);
      auto up = new UnCompress();
      unpacked ~= cast(char[])up.uncompress(packed);
      unpacked ~= cast(char[])up.flush();
    }
    foreach (auto s; unpacked.splitLines) {
      writeln(s);
    }
  }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150204/f6179d71/attachment.sig>


More information about the Digitalmars-d-learn mailing list