我想写一个以二进制从文件读取数据,然后存入一个文本文件的程序,但是现在出现问题,一个300K的dll文件我读到richedit里,存到文本发现变成1.1M了,下面是读文件显示的代码,高手看看有什么问题。或者谁有例子给一个也行
var
F: file;
i, aByte: Byte;
aString: string;
begin
if FileExists(Edit1.Text) then
begin
Button1.Enabled := False;
Screen.Cursor := crHourGlass;
RichEdit1.Lines.Clear;
AssignFile(F, Edit1.Text);
ReSet(F, 1);
try
i := 0;
while not Eof(F) do
begin
BlockRead(F, aByte, 1);
if i = 255 then
begin
RichEdit1.Lines.Add(aString + '+');
i := 0;
end;
if i = 0 then
aString := '';
aString := aString + '#' + IntToStr(aByte);
i := i + 1;
end;
if i > 0 then
RichEdit1.Lines.Add(aString + ';')
else
if FileSize(F) > 0 then
(PChar(RichEdit1.Text) + Length(PChar(RichEdit1.Text)) - 1)^ := ';';
finally
CloseFile(F);
end;
Screen.Cursor := crDefault;
Button1.Enabled := True;
end
else
ShowMessage('File not found!');
end;
var
F: file;
i, aByte: Byte;
aString: string;
begin
if FileExists(Edit1.Text) then
begin
Button1.Enabled := False;
Screen.Cursor := crHourGlass;
RichEdit1.Lines.Clear;
AssignFile(F, Edit1.Text);
ReSet(F, 1);
try
i := 0;
while not Eof(F) do
begin
BlockRead(F, aByte, 1);
if i = 255 then
begin
RichEdit1.Lines.Add(aString + '+');
i := 0;
end;
if i = 0 then
aString := '';
aString := aString + '#' + IntToStr(aByte);
i := i + 1;
end;
if i > 0 then
RichEdit1.Lines.Add(aString + ';')
else
if FileSize(F) > 0 then
(PChar(RichEdit1.Text) + Length(PChar(RichEdit1.Text)) - 1)^ := ';';
finally
CloseFile(F);
end;
Screen.Cursor := crDefault;
Button1.Enabled := True;
end
else
ShowMessage('File not found!');
end;
解决方案 »
- BDE设置问题,高手帮忙.这个问题困扰我很久了.
- 如何将一个类的对象转换成TBytes 类型?
- Bitter_fish(苦鱼) 领分!
- 请问TEdit控件从右边开始显示text内容是怎样实现的?
- 多文档界面操作
- 请教ADOQUERY的SQL语句写法。 UP有分!
- 没在Delphi下做过COM,请给个例子参考一下,要有注释说明,另外不理解Delphi中的COMObject和AutometionObject有什么不同.
- Delphi 2007(with Vista Support)来了!新用点预览
- fast report 条码问题
- 那可以下载 姚庭宝 主编《精通delphi》 电子工业出版社
- 一元回归方程的图形实现
- 没办法,临时换开发语言,兄弟们看看我的 问题!
不是象帮助里那样
var FromF, ToF: file;
NumRead, NumWritten: Integer;
Buf: array[1..2048] of Char;
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
AssignFile(FromF, OpenDialog1.FileName);
Reset(FromF, 1); { Record size = 1 }
if SaveDialog1.Execute then { Display Save dialog box}
begin
AssignFile(ToF, SaveDialog1.FileName); { Open output file } Rewrite(ToF, 1); { Record size = 1 }
//Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF))+ ' bytes...');
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
end;
end;
end;
直接读近来就存出去的,我关键想要中间的显示和转换的过程。
例如 255 Btye类型 长度: 1 字节
'255' String类型 长度: 3 字节
如果通过你做的程序打开的话,可以压缩存储,打开的时候解压。
或者根本不转成txt,直接打开DLL然后转换显示
#77#90#80#0#2#0#0#0#4#0#15#0#255#255#0#0#184#0#0#0#0#0#0#0#64#0#26#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#1#0#0#186#16#0#14#31#180#9#205#33#184#
的形式,那么转换文件必然要比原二进制文件要大。
二进制中每一个Byte的值从0-255,要用文本表示出来就要占用1-3个字符,再加一个#,每个字符一个Byte,也就是二进制的一个Byte转换后要占用2-4个Byte,转换文件平均要比原文件大3.57倍
我那么做只是为了区分,那么直接用hex显示呢?存回去的时候再转成二进制的可以吧,或者有什么别的更好的办法么?
var
i: Integer;
begin
aHexStr := StringReplace(aHexStr, ' ', '', [rfReplaceAll, rfIgnoreCase]);
for i := 1 to Length(aHexStr) do
begin
if Odd(i) then
begin
Result := Result + Char(StrToIntDef('$' + Copy(aHexStr, i, 2), 0));
end;
end;
end;
var
i:integer;
s:string;
begin
assignfile(F , filename);
reset(f , 1);
try
size:=FileSize(F);
getmem(buf , size+1);
BlockRead(F , buf^ ,size);
for i:=0 to size do
begin
s:=s+' '+inttohex(ord(buf[i]),2);
//if (inttohex(ord(buf[i]),2)='aa') or (inttohex(ord(buf[i]),2)='AA') then
//buf[i]:=#0;
end;
form1.memo1.Lines.Add(s);
assignfile(F1 , '1.dat');
Rewrite(f1, 1);
BlockWrite(f1 , buf^ , size);
buf[size]:=#0;
finally
closefile(f);
closefile(f1);
end;
end;
i, pos: integer;
bData: byte;
begin
pos := 0;
// 扫描data,挨个把'#123'形式的字符串转换成byte型数据
for i:=1 to Lenth(data) do
begin
if data[i]='#' then
begin
// 如果pos已经指向了一个'#'则转换pos所指向的'#123'中的数字串为byte数据
if pos<>0 and data[pos]='#' then
begin
bData := StrToInt( Copy(data, pos+1, i-pos-1) );
WriteByteToFile( bData ); // 把转换数据写入文件
end;
pos := i; // pos指向新的一个'#'开头的数字串
end;
end; // 转换最末尾一个 '#153'
if i>pos and pos<>0 and data[pos]='#' then
begin
bData := StrToInt( Copy(data, pos+1, i-pos) );
WriteByteToFile( bData );
end;
end;