下面这段代码是临时写的,只是为了测试方法是否可行,所以代码比较乱,各位将就看吧:)
现在的情况是运行随机出现错误,提示内存存取错误
如果这个代码一次只分析一个文件就没事,如果连续分析多个肯定出错,delphi 7也不告知错误代码,那个cpu调试就出来了,俺看不懂汇编:(谁能帮俺看看是什么原因unit Unit_main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;type
bencoding_type=record
Field:string[255];
BType:(srings,integers,lists,dictionaries,wuxiao);
StrValue:string[255];
IntValue:longint;
end; TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure getbencoding(Filename:string);
var
dchar:char;
bit:array of char;
dmax,dpos:longint;
dstr:string;
tmplist:file of bencoding_type;
procedure logit(text:string);
var
logfile:textfile;
begin
assignfile(logfile,'c:\l.txt');
if fileexists('c:\l.txt') then append(logfile)
else rewrite(logfile);
writeln(logfile,dstr+text);
closefile(logfile);end;function getint:longint;
var
TmpInt:longint;
TmpStr:string;
begin
inc(dpos);
TmpStr:='0';
repeat
TmpStr:=TmpStr+bit[dpos];
inc(dpos);
until bit[dpos]='e';
try
tmpint:=strtoint(tmpstr);
except
end;
inc(dpos);
result:=tmpint;
end;
function readtorrentfile:boolean;
var
bitfile:file of char;
bitbuff:array [0..1023] of char;
bufsize,i:integer;
oldsize:longint;
begin
result:=false;
try
assignfile(bitfile,FileName);
reset(bitfile);
repeat
BlockRead(bitfile, bitbuff, SizeOf(bitbuff), bufsize);
oldsize:=High(bit)-low(bit)+1;
setlength(bit,oldsize+bufsize);
for i:=0 to bufsize do
bit[oldsize+i]:=bitbuff[i];
until (bufsize = 0);
closefile(bitfile);
result:=true;
except
logit('读取文件 '+filename+' 错误');
end;
end;function getstr:string;
var
StrLenS:string;
StrLen:Longint;
TmpInt:integer;
TmpStr:string;
begin
StrLenS:='';
repeat
Strlens:=strlens+bit[dpos];
inc(dpos);
until bit[dpos]=':';
try
strlen:=strtoint(strlens);
except
end;
inc(dpos);
TmpStr:='';
for tmpint:=1 to strlen do begin
if tmpint<255 then
TmpStr:=TmpStr+bit[dpos];
inc(dpos);
end;
result:=tmpstr;
end;
procedure dictionarie;
var
ntype:integer;
tmp_str:bencoding_type;
tc:char;
begin
Application.ProcessMessages;
tc:=bit[dpos];
if (tc<>'d') and (tc<>'l') then begin
tmp_str.Field:=getstr;
tc:=bit[dpos];
logit('字段名:'+tmp_str.Field+'['+tc+']');
end;
case tc of
'0'..'9':begin
logit('发现字符型');
tmp_str.BType:=srings;
tmp_str.StrValue:=getstr;
end;
'i':begin
logit('发现数字型');
tmp_str.BType:=integers;
tmp_str.IntValue:=getint;
end;
'd','l':begin
logit('发现字典型');
logit('开始循环...');
dstr:=dstr+' ';
inc(dpos);
tmp_str.BType:=dictionaries;
repeat
dictionarie;
logit('准备继续循环,类型:'+bit[dpos]);
until bit[dpos]='e';
inc(dpos);
dstr:=copy(dstr,1,length(dstr)-2);
logit('循环结束...');
end;
else
tmp_str.BType:=wuxiao;
end; // end case
logit('处理结束');
case tmp_str.BType of
wuxiao:begin
logit('无效类型-'+tc);
exit;
end;
integers:begin
logit('数值型:'+tmp_str.Field+' - '+inttostr(tmp_str.IntValue));
write(tmplist,tmp_str);
end;
srings:begin
logit('字符型:'+tmp_str.Field+':'+tmp_str.StrValue);
write(tmplist,tmp_str);
end;
end;
end;begin
logit('========================================');
if not readtorrentfile then exit else begin
assignfile(tmplist,'c:\tmplist.txt');
rewrite(tmplist);
dmax:=high(bit)-low(bit)+1;
dpos:=0;
dictionarie;
closefile(tmplist);
logit('关闭');
end;
logit('========================================');
end;procedure TForm1.Button1Click(Sender: TObject);
var
max,i:integer;
tm:tlistitem;
vlist:file of bencoding_type;
t:bencoding_type;
sea:tsearchrec;
err:integer;
fname:string;
begin
i:=0;
if findfirst('C:\Documents and Settings\Administrator\桌面\torrent\*.torrent',faanyfile,sea)=0 then
repeat
fname:='C:\Documents and Settings\Administrator\桌面\torrent\'+sea.Name;
inc(i);
label1.Caption:=inttostr(i)+'-'+fname;
Application.ProcessMessages;
getbencoding(fname);
until findnext(sea)<>0;
end;end.
现在的情况是运行随机出现错误,提示内存存取错误
如果这个代码一次只分析一个文件就没事,如果连续分析多个肯定出错,delphi 7也不告知错误代码,那个cpu调试就出来了,俺看不懂汇编:(谁能帮俺看看是什么原因unit Unit_main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;type
bencoding_type=record
Field:string[255];
BType:(srings,integers,lists,dictionaries,wuxiao);
StrValue:string[255];
IntValue:longint;
end; TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure getbencoding(Filename:string);
var
dchar:char;
bit:array of char;
dmax,dpos:longint;
dstr:string;
tmplist:file of bencoding_type;
procedure logit(text:string);
var
logfile:textfile;
begin
assignfile(logfile,'c:\l.txt');
if fileexists('c:\l.txt') then append(logfile)
else rewrite(logfile);
writeln(logfile,dstr+text);
closefile(logfile);end;function getint:longint;
var
TmpInt:longint;
TmpStr:string;
begin
inc(dpos);
TmpStr:='0';
repeat
TmpStr:=TmpStr+bit[dpos];
inc(dpos);
until bit[dpos]='e';
try
tmpint:=strtoint(tmpstr);
except
end;
inc(dpos);
result:=tmpint;
end;
function readtorrentfile:boolean;
var
bitfile:file of char;
bitbuff:array [0..1023] of char;
bufsize,i:integer;
oldsize:longint;
begin
result:=false;
try
assignfile(bitfile,FileName);
reset(bitfile);
repeat
BlockRead(bitfile, bitbuff, SizeOf(bitbuff), bufsize);
oldsize:=High(bit)-low(bit)+1;
setlength(bit,oldsize+bufsize);
for i:=0 to bufsize do
bit[oldsize+i]:=bitbuff[i];
until (bufsize = 0);
closefile(bitfile);
result:=true;
except
logit('读取文件 '+filename+' 错误');
end;
end;function getstr:string;
var
StrLenS:string;
StrLen:Longint;
TmpInt:integer;
TmpStr:string;
begin
StrLenS:='';
repeat
Strlens:=strlens+bit[dpos];
inc(dpos);
until bit[dpos]=':';
try
strlen:=strtoint(strlens);
except
end;
inc(dpos);
TmpStr:='';
for tmpint:=1 to strlen do begin
if tmpint<255 then
TmpStr:=TmpStr+bit[dpos];
inc(dpos);
end;
result:=tmpstr;
end;
procedure dictionarie;
var
ntype:integer;
tmp_str:bencoding_type;
tc:char;
begin
Application.ProcessMessages;
tc:=bit[dpos];
if (tc<>'d') and (tc<>'l') then begin
tmp_str.Field:=getstr;
tc:=bit[dpos];
logit('字段名:'+tmp_str.Field+'['+tc+']');
end;
case tc of
'0'..'9':begin
logit('发现字符型');
tmp_str.BType:=srings;
tmp_str.StrValue:=getstr;
end;
'i':begin
logit('发现数字型');
tmp_str.BType:=integers;
tmp_str.IntValue:=getint;
end;
'd','l':begin
logit('发现字典型');
logit('开始循环...');
dstr:=dstr+' ';
inc(dpos);
tmp_str.BType:=dictionaries;
repeat
dictionarie;
logit('准备继续循环,类型:'+bit[dpos]);
until bit[dpos]='e';
inc(dpos);
dstr:=copy(dstr,1,length(dstr)-2);
logit('循环结束...');
end;
else
tmp_str.BType:=wuxiao;
end; // end case
logit('处理结束');
case tmp_str.BType of
wuxiao:begin
logit('无效类型-'+tc);
exit;
end;
integers:begin
logit('数值型:'+tmp_str.Field+' - '+inttostr(tmp_str.IntValue));
write(tmplist,tmp_str);
end;
srings:begin
logit('字符型:'+tmp_str.Field+':'+tmp_str.StrValue);
write(tmplist,tmp_str);
end;
end;
end;begin
logit('========================================');
if not readtorrentfile then exit else begin
assignfile(tmplist,'c:\tmplist.txt');
rewrite(tmplist);
dmax:=high(bit)-low(bit)+1;
dpos:=0;
dictionarie;
closefile(tmplist);
logit('关闭');
end;
logit('========================================');
end;procedure TForm1.Button1Click(Sender: TObject);
var
max,i:integer;
tm:tlistitem;
vlist:file of bencoding_type;
t:bencoding_type;
sea:tsearchrec;
err:integer;
fname:string;
begin
i:=0;
if findfirst('C:\Documents and Settings\Administrator\桌面\torrent\*.torrent',faanyfile,sea)=0 then
repeat
fname:='C:\Documents and Settings\Administrator\桌面\torrent\'+sea.Name;
inc(i);
label1.Caption:=inttostr(i)+'-'+fname;
Application.ProcessMessages;
getbencoding(fname);
until findnext(sea)<>0;
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货