我用 C 写了这个程序 http://pastebay.com/182597并且在二郎中 http://pastebay.com/185244
为了练习,我尝试用 D 重写。一个朋友也用 D 写了它,但是写得不同 https://stackoverflow.com/questions/8690759/how-do-i-read-a-bufferedfile-using-readubyte-buffer-when-the-buffer-length-i
步骤很简单。伪代码:
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
我的D尝试:
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(&x, 8, 1, fp)){
writeln("start");
x=swapEndian(x);
writeln(x," ",cnt++,"\n");
ubyte[] arr= new ubyte[x];
fread(&arr, x, 1, fp);
for(i=0;i<x;i++) arr[i]-=1;
std.file.write("/home/fold/wak_oggs/"~to!string(cnt)~".ogg",arr);
}
}
看来我不能只在 arr 上使用 fread 。 sizeof 是 16,当我到达减法部分时,它会出现分段错误。我无法自动分配静态数组,或者至少我不知道如何分配。我似乎也无法使用 malloc,因为当我在循环字节时尝试转换 void* 时,它会给我错误。你会怎么写这个,或者我可以做得更好吗?