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);
}
}
看來我不能只用FREAD上ARR。 sizeof是16,當我到達減法部分時它給出了分段錯誤。我不能自動分配一個靜態數組,或者至少我不知道如何。我也似乎無法使用malloc,因爲當我嘗試在通過字節循環時拋出void *時,它會給出錯誤。你會如何寫這個,或者,我能做得更好?
確定'&arr'指向數組的第一個元素? – hvd 2012-01-02 23:52:41