BlockRead 和 BlockWrite 使用的問題 |
答題得分者是:jow
|
Nelson Lo
一般會員 發表:35 回覆:87 積分:24 註冊:2003-04-04 發送簡訊給我 |
各位先進大家好
小弟在使用BlockRead 和 BlockWrite 出的點問題
其中程式碼如下
procedure TForm1.BlockRW(const FileName: String);
const
BlockSize = 1024*128;
var
u_Low_f : file of Byte;
VoiceData : array[0..BlockSize] of Smallint;
i : longint;
Remain, ReadCount, GotCount, NumWritten : integer;
ReadDataIndex : Longword;
RealData : array of Smallint; // 轉換過的資料
TotalPointNum : Longword;
begin
ReadDataIndex := 0;
AssignFile(u_Low_f, FileName);
Reset(u_Low_f);
try
Remain := FileSize(u_Low_f);
TotalPointNum := Remain;
SetLength(RealData, TotalPointNum); while Remain > 0 do
begin
if Remain < BlockSize then
ReadCount := Remain
else
ReadCount := BlockSize; BlockRead(u_Low_f, VoiceData, ReadCount, GotCount);
if ReadCount <> GotCount then
Raise Exception.Create('READ FILE ERROR !!'); for i := 0 to GotCount -1 do
begin
RealData[ReadDataIndex] := VoiceData[i] 1;
inc(ReadDataIndex);
end;
Dec(Remain,GotCount); BlockWrite(u_Low_f, VoiceData, GotCount, NumWritten);
if NumWritten <> GotCount then
Raise Exception.Create('WRITE FILE ERROR !!');
end;
finally
CloseFile(u_Low_f);
end;
end; 其目的只是要把檔案的內容讀入後
把每一個byte加1之後在寫回去
但是現在BlockRead在讀了幾次之後
就會發生ReadCount <> GotCount 的狀況
但是如果不執行BlockWrite就不會發生
有哪位大大能指教一下嗎
謝謝
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
BlockRead 和 BlockWrite 的讀寫單位是Byte,而不是Record Count
我想你的程式碼有點弄混了. 另外根據你的程式需求,如果用下列的方式應該會簡潔一點 procedure TfrmRealKernel.DataRW(const FileName: string);
var
F1, F2: TFileStream;
I, R: Integer;
Buff: array[0..9] of Smallint;
begin
if FileExists(FileName) then
begin
F1 := TFileStream.Create(FileName, fmOpenRead);
try
F2 := TFileStream.Create('RESULTFILE', fmOpenRead fmOpenWrite);
try
while F1.Position < F1.Size do
begin
R := F1.Read(Buff, SizeOf(Buff));
if R > 0 then
begin
for I := 0 to (R div SizeOf(Smallint))-1 do
Buff[I] := Buff[I] 1;
F2.Write(Buff, R);
end;
end;
finally
FreeAndNil(F2);
end;
finally
FreeAndNil(F1);
end;
end;
end; 檔案不大時,直接載入記憶體中處理... procedure TfrmRealKernel.DataRW2(const FileName: string);
var
P: PSmallint;
M: TMemoryStream;
I, Count: Integer;
begin
if FileExists(FileName) then
begin
M := TMemoryStream.Create;
try
M.LoadFromFile(FileName);
Count := M.Size div SizeOf(Smallint);
P := PSmallint(M.Memory);
for I := 0 to Count-1 do
begin
P^ := P^ 1;
Inc(P);
end;
M.SaveToFile(FileName);
finally
FreeAndNil(M);
end;
end;
end;
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
BlockRead 和 BlockWrite 的讀寫單位是Byte,而不是Record Count 我想你的程式碼有點弄混了. 另外根據你的程式需求,如果用下列的方式應該會簡潔一點 procedure TfrmRealKernel.DataRW(const FileName: string); var F1, F2: TFileStream; I, R: Integer; Buff: array[0..9] of Smallint; begin if FileExists(FileName) then begin F1 := TFileStream.Create(FileName, fmOpenRead); try F2 := TFileStream.Create('RESULTFILE', fmOpenRead fmOpenWrite); try while F1.Position < F1.Size do begin R := F1.Read(Buff, SizeOf(Buff)); if R > 0 then begin for I := 0 to (R div SizeOf(Smallint))-1 do Buff[I] := Buff[I] 1; F2.Write(Buff, R); end; end; finally FreeAndNil(F2); end; finally FreeAndNil(F1); end; end; end; 檔案不大時,直接載入記憶體中處理... procedure TfrmRealKernel.DataRW2(const FileName: string); var P: PSmallint; M: TMemoryStream; I, Count: Integer; begin if FileExists(FileName) then begin M := TMemoryStream.Create; try M.LoadFromFile(FileName); Count := M.Size div SizeOf(Smallint); P := PSmallint(M.Memory); for I := 0 to Count-1 do begin P^ := P^ 1; Inc(P); end; M.SaveToFile(FileName); finally FreeAndNil(M); end; end; end; |
Nelson Lo
一般會員 發表:35 回覆:87 積分:24 註冊:2003-04-04 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |