一个奇怪的问题,请各位大虾帮助!(200高分重谢)(200分)

  • 主题发起人 主题发起人 yousp
  • 开始时间 开始时间
Y

yousp

Unregistered / Unconfirmed
GUEST, unregistred user!
[:(!]偶想使用Delphi来读取某个Web服务器的Log文件,此时该服务器已经打开了该Log文件,<br>并且不断将内容写入该文件,我用了以下好几种方法尝试均无法打开该文件。但是,<br>我使用Notepad或者Editplus编辑器却可以将其打开,不知道他们使用的是什么API?<br><br>我用的方法:(我开始直接使用TMemo.lines.loadFromFile(fileName)去打开该文件,<br>结果失败,因此使用TMemoryStream去打开,仍然失败,最后使用TFileStream还是失败)[h1][/h1]<br><br>程序如下:<br>unit FrmMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, Menus, StdCtrls, ExtCtrls;<br><br>type<br>&nbsp; TFormMain = class(TForm)<br>&nbsp; &nbsp; MemoConsole: TMemo;<br>&nbsp; &nbsp; MainMenu: TMainMenu;<br>&nbsp; &nbsp; TopMenuFile: TMenuItem;<br>&nbsp; &nbsp; MenuItemOpen: TMenuItem;<br>&nbsp; &nbsp; MenuItemExit: TMenuItem;<br>&nbsp; &nbsp; OpenDialog: TOpenDialog;<br>&nbsp; &nbsp; Timer: TTimer;<br><br>&nbsp; &nbsp; procedure MenuItemOpenClick(Sender: TObject);<br>&nbsp; &nbsp; procedure TimerTimer(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; FLogFileName: String;<br>&nbsp; &nbsp; FBuf: TMemoryStream;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; function GetStream(const FileName: String): Boolean;<br>&nbsp; end;<br><br>var<br>&nbsp; FormMain: TFormMain;<br><br>implementation<br><br>{$R *.dfm}<br><br>//从FLogFileName获取一个TStream<br>function TFormMain.GetStream(const FileName: String): Boolean;<br>var<br>&nbsp; FileStream: TFileStream;<br>begin<br>&nbsp; if not FileExists(FileName) then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := false;<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; try<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareCompat);<br>&nbsp; &nbsp; &nbsp; FBuf.LoadFromStream(FileStream);<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; MessageBox(Handle, PChar('无法打开文件:' + FLogFileName),<br>&nbsp; &nbsp; &nbsp; &nbsp; '打开日志文件错误', MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp; Result := false;<br>&nbsp; &nbsp; &nbsp; Timer.Enabled := false;<br>&nbsp; &nbsp; &nbsp; FileStream.Free;<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; FileStream.Free;<br>&nbsp; end;<br>&nbsp; Result := true;<br>end;<br><br>procedure TFormMain.MenuItemOpenClick(Sender: TObject);<br>begin<br>&nbsp; with OpenDialog do<br>&nbsp; begin<br>&nbsp; &nbsp; Filter := '日志文件(*.log)|*.log|所有文件(*.*)|*.*';<br>&nbsp; &nbsp; if Execute then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; FLogFileName := FileName;<br>&nbsp; &nbsp; &nbsp; Timer.Enabled := true;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure TFormMain.TimerTimer(Sender: TObject);<br>begin<br>&nbsp; if GetStream(FLogFileName) then<br>&nbsp; begin<br>&nbsp; &nbsp; MemoConsole.Lines.LoadFromStream(FBuf);<br>&nbsp; &nbsp; Timer.Enabled := false;<br>&nbsp; end;<br>end;<br><br>procedure TFormMain.FormCreate(Sender: TObject);<br>begin<br>&nbsp; FBuf := TMemoryStream.Create;<br>end;<br><br>procedure TFormMain.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; FBuf.Free;<br>end;<br><br>end.
 
timer的interval设长些,另外你什么时候把Timer的enable设成true,怎么都是false?
 
我在打开文件以后有一个Timer.enabled := true
 
TFileStream.Create(你的Log文件, fmOpenRead or fmShareDenyNone);
 
to Another_eYes:<br><br>这个我也试过了,能不能给出一个能够运行的代码?万分感谢!
 
怎么没有人回答呀?
 
多人接受答案了。
 
后退
顶部