title: What is the D way to write this?
body: <p><a href="http://pastebay.com/182597" rel="nofollow noreferrer">I wrote this program in C</a> and also <a href="http://pastebay.com/185244" rel="nofollow noreferrer">in erlang</a></p>
  
  <p>To practice I tried to rewrite in D. A friend also wrote it in D but <a href="https://stackoverflow.com/questions/8690759/how-do-i-read-a-bufferedfile-using-readubyte-buffer-when-the-buffer-length-i">wrote it differently</a></p>
  
  <p><strong>The steps are simple. Pseudocode:</strong></p>
  
  <pre><code>While not end of file:
     X = Read ulong from file and covert to little endian
     Y = Read X bytes from file into ubyte array
     subtract 1 from each byte in Y
     save Y as an ogg file
  </code></pre>
  
  <p><strong>My D attempt:</strong></p>
  
  <pre><code>import std.file, std.stdio, std.bitmanip, std.conv, core.stdc.stdio : fread;
  void main(){
    auto file = File("./sounds.pk", "r+");
    auto fp = file.getFP();
    ulong x;
    int i,cnt;
    while(fread(&amp;x, 8, 1, fp)){
       writeln("start");
       x=swapEndian(x);
       writeln(x," ",cnt++,"\n");
       ubyte[] arr= new ubyte[x]; 
       fread(&amp;arr, x, 1, fp);
       for(i=0;i&lt;x;i++) arr[i]-=1;
       std.file.write("/home/fold/wak_oggs/"~to!string(cnt)~".ogg",arr);
    }   
  }
  </code></pre>
  
  <p>It seems I can't just use fread on arr. sizeof is 16 and it gives segmentation fault when I get to the subtracting part. I can't auto alloc a static array, or at least I don't know how. I also can't seem to use malloc because it gives me errors when I try to cast the void* when I loop through the bytes. How would you write this, or, what could I do better?</p>