F
fengzhongxiayin
Unregistered / Unconfirmed
GUEST, unregistred user!
程序是将三个文件写入流中,再从流中读出来。
问题1:在最后seek(sofromcurrent,filesize);,为什么是这样,应该是seek(filesize,sofromcurrent);sofromcurrent应放到后边呀
问题2:stream.write(i,4)是什么意思是向stream中写入i中的内容吗?
filename[0]是什么?是不是为空?
tfileinfo=packed record
[red]filename:string[255];[/red]
filesize:integer;
filedata:tmemorystream;
end;
tfilesinfo=array of tfileinfo;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
path:string;
procedure filesinfotostream(filesinfo:tfilesinfo;stream:tstream);
function streamtofilesinfo(stream:tstream):tfilesinfo;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure tform1.filesinfotostream(filesinfo:tfilesinfo;stream:tstream);
var
i:integer;
begin
i:=length(filesinfo);
[red]stream.Write(i,4);[/red]
for i:=low(filesinfo) to high(filesinfo) do
with filesinfo,stream do
begin
write(filename[1],sizeof(filename)-1);
write(filesize,sizeof(filesize));
copyfrom(filedata,filedata.size);
end;
stream.position:=0;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
path:='c:/';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
filesinfo:tfilesinfo;
savestream:tmemorystream;
begin
setlength(filesinfo,3);
for i:=low(filesinfo) to high(filesinfo) do
with filesinfo do
begin
case i of
0:filename:='1.gif';
1:filename:='2.htm';
2:filename:='3.jpg';
end;
filedata:=tmemorystream.Create;
filedata.LoadFromfile(path+filename);
filesize:=filedata.Size;
end;
savestream:=tmemorystream.Create;
filesinfotostream(filesinfo,savestream);
savestream.SaveTofile(path+'bqy.data');
for i:=low(filesinfo) to high(filesinfo) do
filesinfo.filedata.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
filesinfo:tfilesinfo;
loadstream:tmemorystream;
begin
loadstream:=tmemorystream.Create;
loadstream.LoadFromFile(path+'bqy.data');
filesinfo:=streamtofilesinfo(loadstream);
loadstream.Free;
for i:=low(filesinfo) to high(filesinfo) do
with filesinfo do
begin
filedata.SaveToFile(path+'copy_'+trim(filename));
filedata.Free;
end;
end;
function tform1.streamtofilesinfo(stream:tstream):tfilesinfo;
var
i:integer;
buffer:string[255];
begin
stream.Position:=0;
stream.Read(i,4);
setlength(result,i);
for i:=low(result) to high(result) do
with result,stream do
begin
read(buffer[1],sizeof(buffer)-1);
filename:=buffer;
read(filesize,4);
filedata:=tmemorystream.Create;
filedata.CopyFrom(stream,filesize);
seek(sofromcurrent,filesize);[blue][/blue]
end;
end;
end.
问题1:在最后seek(sofromcurrent,filesize);,为什么是这样,应该是seek(filesize,sofromcurrent);sofromcurrent应放到后边呀
问题2:stream.write(i,4)是什么意思是向stream中写入i中的内容吗?
filename[0]是什么?是不是为空?
tfileinfo=packed record
[red]filename:string[255];[/red]
filesize:integer;
filedata:tmemorystream;
end;
tfilesinfo=array of tfileinfo;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
path:string;
procedure filesinfotostream(filesinfo:tfilesinfo;stream:tstream);
function streamtofilesinfo(stream:tstream):tfilesinfo;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure tform1.filesinfotostream(filesinfo:tfilesinfo;stream:tstream);
var
i:integer;
begin
i:=length(filesinfo);
[red]stream.Write(i,4);[/red]
for i:=low(filesinfo) to high(filesinfo) do
with filesinfo,stream do
begin
write(filename[1],sizeof(filename)-1);
write(filesize,sizeof(filesize));
copyfrom(filedata,filedata.size);
end;
stream.position:=0;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
path:='c:/';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
filesinfo:tfilesinfo;
savestream:tmemorystream;
begin
setlength(filesinfo,3);
for i:=low(filesinfo) to high(filesinfo) do
with filesinfo do
begin
case i of
0:filename:='1.gif';
1:filename:='2.htm';
2:filename:='3.jpg';
end;
filedata:=tmemorystream.Create;
filedata.LoadFromfile(path+filename);
filesize:=filedata.Size;
end;
savestream:=tmemorystream.Create;
filesinfotostream(filesinfo,savestream);
savestream.SaveTofile(path+'bqy.data');
for i:=low(filesinfo) to high(filesinfo) do
filesinfo.filedata.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
filesinfo:tfilesinfo;
loadstream:tmemorystream;
begin
loadstream:=tmemorystream.Create;
loadstream.LoadFromFile(path+'bqy.data');
filesinfo:=streamtofilesinfo(loadstream);
loadstream.Free;
for i:=low(filesinfo) to high(filesinfo) do
with filesinfo do
begin
filedata.SaveToFile(path+'copy_'+trim(filename));
filedata.Free;
end;
end;
function tform1.streamtofilesinfo(stream:tstream):tfilesinfo;
var
i:integer;
buffer:string[255];
begin
stream.Position:=0;
stream.Read(i,4);
setlength(result,i);
for i:=low(result) to high(result) do
with result,stream do
begin
read(buffer[1],sizeof(buffer)-1);
filename:=buffer;
read(filesize,4);
filedata:=tmemorystream.Create;
filedata.CopyFrom(stream,filesize);
seek(sofromcurrent,filesize);[blue][/blue]
end;
end;
end.