procedure TForm1.Button1Click(Sender: TObject); var iFileHandle1,iFileHandle:integer; iFileLength,iBytesRead:integer; pBuffer:pChar; begin iFileHandle1:=FileOpen('file1',fmOpenWrite); if iFileHandle1>0 then FileSeek(iFileHandle,0,2); else iFileHandle1:=FileCreate('file1'); iFileHandle2:=FileOpen('file2',fmOpenRead); iFileLength := FileSeek(iFileHandle2,0,2); FileSeek(iFileHandle2,0,0); Buffer := PChar(AllocMem(iFileLength + 1)); iBytesRead = FileRead(iFileHandle2, Buffer, iFileLength); FileWrite(iFileHandle1,Buffer,iBytesRead); FileClose(iFileHandle2); FileClose(iFileHandle1); end;
刚才的代码好象不行,试试这个,一定行。 procedure TForm1.Button1Click(Sender: TObject); var iFileHandle1,iFileHandle2:integer; iBytesRead:integer; pBuffer:array [0..1024] of Char; begin OpenDialog1.Execute; OpenDialog2.Execute; iFileHandle1:=FileOpen(OpenDialog1.FileName,fmOpenWrite); if iFileHandle1>0 then FileSeek(iFileHandle1,0,2) else iFileHandle1:=FileCreate(OpenDialog1.FileName); iFileHandle2:=FileOpen(OpenDialog2.FileName,fmOpenRead); iBytesRead:=FileRead(iFileHandle2, pBuffer, 1024); while iBytesRead>0 do begin FileWrite(iFileHandle1,pBuffer,iBytesRead); iBytesRead := FileRead(iFileHandle2, pBuffer, 1024); end; FileClose(iFileHandle2); FileClose(iFileHandle1); end;
看看这个函数也许对你有用 Function Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean; var Target,Source:TFileStream; MyFileSize:integer; begin try Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareExclusive); Target:=TFileStream.Create(TargetFile,fmOpenWrite or fmShareExclusive); try Target.Seek(0,soFromEnd);//往尾部添加资源 Target.CopyFrom(Source,0); MyFileSize:=Source.Size+Sizeof(MyFileSize);//计算资源大小,并写入辅程尾部 Target.WriteBuffer(MyFileSize,sizeof(MyFileSize)); finally Target.Free; Source.Free; end; except Result:=False; Exit; end; Result:=True; end;
var
iFileHandle1,iFileHandle:integer;
iFileLength,iBytesRead:integer;
pBuffer:pChar;
begin
iFileHandle1:=FileOpen('file1',fmOpenWrite);
if iFileHandle1>0 then
FileSeek(iFileHandle,0,2);
else
iFileHandle1:=FileCreate('file1'); iFileHandle2:=FileOpen('file2',fmOpenRead);
iFileLength := FileSeek(iFileHandle2,0,2);
FileSeek(iFileHandle2,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead = FileRead(iFileHandle2, Buffer, iFileLength); FileWrite(iFileHandle1,Buffer,iBytesRead);
FileClose(iFileHandle2);
FileClose(iFileHandle1);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
iFileHandle1,iFileHandle2:integer;
iBytesRead:integer;
pBuffer:array [0..1024] of Char;
begin
OpenDialog1.Execute;
OpenDialog2.Execute;
iFileHandle1:=FileOpen(OpenDialog1.FileName,fmOpenWrite);
if iFileHandle1>0 then
FileSeek(iFileHandle1,0,2)
else
iFileHandle1:=FileCreate(OpenDialog1.FileName); iFileHandle2:=FileOpen(OpenDialog2.FileName,fmOpenRead);
iBytesRead:=FileRead(iFileHandle2, pBuffer, 1024);
while iBytesRead>0 do
begin
FileWrite(iFileHandle1,pBuffer,iBytesRead);
iBytesRead := FileRead(iFileHandle2, pBuffer, 1024);
end;
FileClose(iFileHandle2);
FileClose(iFileHandle1);
end;
Function Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean;
var
Target,Source:TFileStream;
MyFileSize:integer;
begin
try
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareExclusive);
Target:=TFileStream.Create(TargetFile,fmOpenWrite or fmShareExclusive);
try
Target.Seek(0,soFromEnd);//往尾部添加资源
Target.CopyFrom(Source,0);
MyFileSize:=Source.Size+Sizeof(MyFileSize);//计算资源大小,并写入辅程尾部
Target.WriteBuffer(MyFileSize,sizeof(MyFileSize));
finally
Target.Free;
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;