[Delphi] 两个类似的二进制文件读写Function ReadFileToBuffer(Filename : String) : PChar;
var
F : File;
Buf: PChar ;
Size : Integer ;
begin
Assignfile(F, filename);
Reset(f, 1);
try
Size := FileSize(F);
Getmem(Buf, Size + 1);
BlockRead(F, Buf^, Size);
Buf[Size]:=#0;
finally
Closefile( F );
Result := Buf ;
end;
end;
procedure WriteBufferToFile(Filename : String; Buf : PChar);
var
WS : array [0..1024] of Char;
F : File ;
YX, WX, Len:integer;
begin
AssignFile(F, Filename) ;
ReWrite(F) ;
YX:=0;
Len := Length(Buf) ;
Try
While YX < Len do
begin
WX := len - YX;
if WX > 1024 then WX := 1024;
strlcopy(WS, @buf[YX], WX);
BlockWrite(F, WS, WX);
YX := YX + WX;
end;
finally
CloseFile(F) ;
end;
end;
Procedure WriteBinaryFile(FileName, OperStr : string) ;
var
FS : TFileStream ;
Buf : PChar ;
Size : LongInt ;
begin
Try
GetMem(Buf, Length(OperStr)) ;
StrPCopy(Buf, OperStr);
Size := Sizeof(Buf) ;
FS := TFileStream.Create(FileName, fmCreate);
FS.Write(Buf, Size);
finally
FS.Free ;
end;
end;
Function ReadBinaryFile(FileName : String) : String ;
Var
FS : TFileStream ;
Buf : PChar ;
begin
Try
Result := '';
FS := TFileStream.Create(FileName, fmOpenRead);
FS.Read(Buf, Sizeof(FS));
finally
FS.Free ;
Result := Buf ;
end;
end;