【发布时间】:2012-01-03 08:54:19
【问题描述】:
I wrote this program in C 和in erlang
为了练习,我尝试用 D 重写。一个朋友也用 D 写过,但是wrote it differently
步骤很简单。伪代码:
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* 时,它会给我带来错误。你会怎么写这个,或者,我能做些什么更好?
【问题讨论】:
-
你确定
&arr指向数组的第一个元素吗?
标签: d