一个简单问题(50分)

  • 主题发起人 主题发起人 knmfkr
  • 开始时间 开始时间
K

knmfkr

Unregistered / Unconfirmed
GUEST, unregistred user!
<br>请问大虾:<br><br>我这样写:<br>if OpenDialog1.Execute then<br>memo1.Lines.LoadFromFile(OpenDialog1.filename)<br>memo1筐里面可以得到文本,可是如果所打开的文件不是文本格式的,就没有办法打开了<br><br>希望:<br>对于文本文件,在memo1里面得到文本;<br>对于非文本文件,在memo1里面得到二进制代码;<br><br><br><br>
 
override the method: loadfromfile
 
思路:<br>1、检验是否为文本文件,可以接受的后缀是TXT HTML HTM ASP 等,如果不是则另外的算法。<br>2、FUNCTION STRTOBIN(S:字符串):字符串;<br>&nbsp; &nbsp;BEGIN<br>&nbsp; &nbsp; &nbsp;将各个字读出,转换为二进制,然后转为字符串,联结起来。。。OK<br>&nbsp; &nbsp;END;<br>3、写入STREAM<br>4、MEMO.loadfromstream.....<br><br>
 
to loopy:<br>能否给一个例子,具体如何将各个字读出,在转为二进制<br>
 
看看这个<br><br>这是一个注册EXE,OBJ,BIN三种类型文件当其被RichEdit打开时会自动转换为16进制显示的例子<br><br>第一:要从TCoriversion派生出一个新类<br>第二:重载CorrvertReadStream函数<br>第三:在主窗体的OnCreate函数中登记文件类型。用RichEdit的.RegisterConversionFormat函数<br><br><br>unit main;<br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,<br>&nbsp; Menus, StdCtrls, ComCtrls;<br><br>type<br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; MainMenu1: TMainMenu;<br>&nbsp; &nbsp; MenuFile: TMenuItem;<br>&nbsp; &nbsp; MenuOpen: TMenuItem;<br>&nbsp; &nbsp; MenuSaveAs: TMenuItem;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; MenuExit: TMenuItem;<br>&nbsp; &nbsp; OpenDlg: TOpenDialog;<br>&nbsp; &nbsp; SaveDlg: TSaveDialog;<br>&nbsp; &nbsp; Rich: TRichEdit;<br>&nbsp; &nbsp; StatusBar: TStatusBar;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure MenuOpenClick(Sender: TObject);<br>&nbsp; &nbsp; procedure MenuSaveAsClick(Sender: TObject);<br>&nbsp; &nbsp; procedure MenuExitClick(Sender: TObject);<br>&nbsp; &nbsp; procedure MenuPopupPopup(Sender: TObject);<br>&nbsp; &nbsp; procedure MenuSelectAllClick(Sender: TObject);<br>&nbsp; &nbsp; procedure MenuCopyClick(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; procedure Progress(Address:LongInt);<br>&nbsp; end;<br><br>var<br>&nbsp; MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>type<br>&nbsp; THexConversion = class(TConversion)<br>&nbsp; public<br>&nbsp; &nbsp; function ConvertReadStream(Stream:TStream; Buffer:PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BufSize:integer): integer; override;<br>&nbsp; end;<br><br><br>// This implements a callback procedure used by TRichEdit when loading<br>// a file. &nbsp;Gets called repeatedly until stream is empty.<br>//<br>function THexConversion.ConvertReadStream(Stream:TStream; Buffer:PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufSize:integer): integer;<br>var s:string;<br>&nbsp; &nbsp; buf:array[1..16] of char;<br>&nbsp; &nbsp; i,n:integer;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; if BufSize &lt;= 82 then Exit;<br>&nbsp; s := Format(';%.5x &nbsp;',[Stream.Position]);<br>&nbsp; n := Stream.Read(buf,16);<br>&nbsp; if n = 0 then Exit;<br>&nbsp; for i := 1 to n do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; AppendStr(s,IntToHex(ord(buf),2)+' ');<br>&nbsp; &nbsp; &nbsp; if i mod 4 = 0 then AppendStr(s,' ');<br>&nbsp; &nbsp; end;<br>&nbsp; AppendStr(s,StringOfChar(' ',62-length(s)));<br>&nbsp; for i := 1 to n do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (buf &lt; #32) or (buf &gt; #126) then<br>&nbsp; &nbsp; &nbsp; &nbsp; buf := '.';<br>&nbsp; &nbsp; &nbsp; AppendStr(s,buf);<br>&nbsp; &nbsp; end;<br>&nbsp; AppendStr(s,#13#10);<br>&nbsp; StrPCopy(Buffer,s);<br>&nbsp; Result := length(s);<br>&nbsp; if Stream.Position and $FFF = 0 then MainForm.Progress(Stream.Position);<br>end;<br><br>procedure TMainForm.Progress(Address:LongInt);<br>begin<br>&nbsp; StatusBar.SimpleText := 'Reading... &nbsp;$'+IntToHex(Address,5);<br>&nbsp; StatusBar.Update;<br>end;<br><br>procedure TMainForm.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Rich.RegisterConversionFormat('bin',THexConversion);<br>&nbsp; Rich.RegisterConversionFormat('obj',THexConversion);<br>&nbsp; Rich.RegisterConversionFormat('exe',THexConversion);<br>end;<br><br>procedure TMainForm.MenuOpenClick(Sender: TObject);<br>var fname:string;<br>begin<br>&nbsp;if OpenDlg.Execute then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp;Screen.Cursor := crHourglass;<br>&nbsp; &nbsp; &nbsp; &nbsp;fname := ExtractFileName(OpenDlg.Filename);<br>&nbsp; &nbsp; &nbsp; &nbsp;StatusBar.SimpleText := 'Reading...';<br>&nbsp; &nbsp; &nbsp; &nbsp;Rich.Lines.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Rich.Lines.LoadFromFile(OpenDlg.Filename);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StatusBar.SimpleText := fname;<br>&nbsp; &nbsp; &nbsp; &nbsp;except on E:EFOpenError do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StatusBar.SimpleText := '';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MessageDlg(Format('Can''t open file %s.',[fname]),mtError,[mbOk],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp;Screen.Cursor := crDefault;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;end;<br>end;<br><br><br>procedure TMainForm.MenuSaveAsClick(Sender: TObject);<br>begin<br>&nbsp; SaveDlg.Filename := ChangeFileExt(OpenDlg.FileName,'.txt');<br>&nbsp; if SaveDlg.Execute then<br>&nbsp; &nbsp; Rich.Lines.SaveToFile(SaveDlg.FileName);<br>end;<br><br>procedure TMainForm.MenuExitClick(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br>end.
 
大概应该是这样的(未经调试)<br>while not eof(f) do<br>var mychar:char<br>begin<br>&nbsp; mychar = read(f);<br>&nbsp; memo1.text = memo1.text + inttohex(ord(mychar),2);<br>end;
 
多人接受答案了。
 
后退
顶部