文件流啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(50分)

  • 主题发起人 主题发起人 花 儿
  • 开始时间 开始时间
try
open.Size:=pb.Max;
open.Position:=Pb.Position;
RichEdit.lines.openfromStream(open);//错了,应该是RichEdit.lines.loadfromStream(open);
finally
open.Free;
end;

这样就应该没问题了,我已经测试过,顺利读出!
 
不是出在 openfromStream,这个是笔误,否则的话编译都不行
fmOpenRead 配上 open.Size:=pb.Max; 就是错误:“拒绝访问“
 
同意pipi,你以只读的方式(fmOpenRead)打开,再来设置文件长度,当然会出错了。
 
我现在的代码是----

var
Open:TFileStream;
d:integer;
begin
if openDlg.Execute=true then
begin
open:=TFileStream.Create(openDlg.FileName,fmOpenRead);
try
//open.Size:=pb.Max;
//open.Position:=Pb.Position;
//showmessage(inttostr(open.Position));
for d:=0 to open.Size do
begin
pb.Position:=open.Position;
pb.StepIt;
end;
RichEdit.lines.LoadfromStream(Open);
finally
Open.Free;
end;
end;
end;

我要实现打开文件的时候 进度条要显示过程,你们帮帮我看看。我觉得读大的文件
用躲线程比较好,不然会死锁一段时间。
 
我不是很理解
pb.Position:=open.Position;
赋值一下就算读入了吗?

如果是的话, pb.StepIt 后面加一个:Application.ProcessMessages();
 
Pipi.----
当然不是的,我要用循环啊~~~~
 
就是啊,你
for d:=0 to open.Size do
begin
pb.Position:=open.Position;
pb.StepIt;
end;
就叫读入啊,我一时想不起来他怎么就算读入了呢
 
循环里面,
pb.Position:=open.Position 有读入功能吗?
pb.StepIt 有读入功能吗?
 
你有没有类事的源代码我看看?
 
其他不说就这句话 if openDlg.Execute=true then 我觉得不好
只要 if openDlg.Execute then

 
open:=TFileStream.Create(openDlg.FileName,fmOpenRead);

RichEdit.lines.LoadfromStream(Open);//读入文件实际上只有这一句,你没有办法
实现进度条的。
 
procedure TForm1.Button4Click(Sender: TObject);
var
MyFile:THandle;
//PacketSize:DWORD;
dwBytesRead:DWORD;
MyResult:Bool;
MyFileSize:integer;
MyBuf:array[0..1023] of char;
i,iCount,iMore,iReadSize:integer;
begin
iReadSize:=0;
if OpenDialog1.Execute then
begin
MyFileSize:=GetFileSize(OpenDialog1.FileName);
MyFile := CreateFile(Pchar(OpenDialog1.FileName),
GENERIC_READ,
FILE_SHARE_READ,
nil,
OPEN_EXISTING,
0,
0);
if MyFile=null then Exit;

WSocketSize.Proto := 'udp';
WSocketSize.Addr := '255.255.255.255';
WSocketSize.Port := '777';
WSocketSize.LocalPort := '0';
WSocketSize.Connect;

WSocketSize.SendStr('filei'+InttoStr(MyFileSize)+'!'+ExtractFileName(OpenDialog1.FileName));
//WSocketSize.Close;
end
else
Exit;

WSocketClient.Proto := 'udp';
WSocketClient.Addr := '255.255.255.255';
WSocketClient.Port := '666';
WSocketClient.LocalPort := '0';
WSocketClient.Connect;

iCount:=MyFileSize div 1024;
iMore:=MyFileSize mod 1024;
MyBuf:= #0;

for i:=1 to iCount do
begin
Gauge1.Progress := Round(iReadSize/MyFileSize * 100);
MyResult:=ReadFile(MyFile,
MyBuf,
1024,
dwBytesRead,
nil);
if Not(MyResult) then Exit;
iReadSize:=iReadSize+1024;
try
WSocketClient.Send(@MyBuf, 1024);
Application.ProcessMessages;
sleep(5);
except
Break;
end;
end;

if iMore > 0 then
begin
Sleep(1);
MyResult:=ReadFile(MyFile,
MyBuf,
iMore,
dwBytesRead,
nil);
if Not(MyResult) then Exit;
iReadSize:=iReadSize+iMore;
WSocketClient.Send(@MyBuf, iMore);
end;

sleep(2);
WSocketSize.SendStr('close');
CloseHandle(MyFile);
WSocketSize.Close;
WSocketClient.Close;

Gauge1.Progress :=100;
//MessageBox(Handle ,Pchar(intTostr(iReadSize)),'',0);
//Showmessage(inttostr(iReadSize));
end;
 
上面是API版本
流的应该用:
procedure TForm1.Button4Click(Sender: TObject);
var
MyFile:TFileStream;
MySize:String;
MyBuf:array[0..1023] of char;
i,iCount,iMore,k:integer;
begin
k:=0;
if OpenDialog1.Execute then
begin
MyFile:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead or fmShareDenyNone);
MySize:=InttoStr(MyFile.Size);

WSocketSize.Proto := 'udp';
WSocketSize.Addr := '255.255.255.255';
WSocketSize.Port := '777';
WSocketSize.LocalPort := '0';
WSocketSize.Connect;
WSocketSize.SendStr(MySize+'!'+ExtractFileName(OpenDialog1.FileName));
WSocketSize.Close;

end
else
Exit;
WSocketClient.Proto := 'udp';
WSocketClient.Addr := '255.255.255.255';
WSocketClient.Port := '666';
WSocketClient.LocalPort := '0';
WSocketClient.Connect;

iCount:=MyFile.Size div 1024;
iMore:=MyFile.Size mod 1024;
MessageBox(Handle,Pchar(inttostr(iCount)+#13+ inttostr(iMore)),'',0);
MyBuf:= #0;


for i:=1 to iCount do
begin
Gauge1.Progress := Round(K/MyFile.Size * 100);
MyFile.ReadBuffer(MyBuf, 1024);
k:=k+1024;
try
WSocketClient.Send(@MyBuf, 1024);
Application.ProcessMessages;
sleep(15);
except
Showmessage('Error');
Break;
end;
end;

if iMore > 0 then
begin
Sleep(1);
MyFile.ReadBuffer(MyBuf, iMore);
k:=k+iMore;
Gauge1.Progress := Round(K/MyFile.Size * 100);
WSocketClient.Send(@MyBuf, iMore);
end;

MyFile.Free;
WSocketClient.Close;
Showmessage(inttostr(k));
end;
 
后退
顶部