函数调用返回无结果,高手看看哪里出错了?(10)

  • 主题发起人 wangyb32
  • 开始时间
W

wangyb32

Unregistered / Unconfirmed
GUEST, unregistred user!
我已经从C盘中的读出下面这条字符串放到demo1中了,00386nam0 2200157 45 005001700000010002200017100004100039101000800080105001800088106000600106200004700112210002100159300001300180300001300193801002200206a20090511153111 a978-7-5064-4866-6 a20090511d尚锦 ekmy0chiy0120 ea achi ay z 000yy ar1 a百吃不厌的点心9bai chi bu yan de dian xin acd尚锦好吃系列 aCNY19.80 a颜金满著 然后调用函数截取指定的字段,放到demo2,没有结果。是哪里出问题了。-下面是代码----------------------------------------------------------------------------------------------------------------------------------unit Unit2;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm2 = class(TForm) Button1: TButton; memo1: TMemo; memo2: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } function marcsplit(m,fid,subfid:string):string; end;var Form2: TForm2;implementationfunction TForm2.marcsplit(m,fid,subfid:string):string;var x,jsq,jlcd,zdcd,zddz,qsdz,zds,wz1,wz2:integer; a,mcq,sjq,zdh,zzd_string,string1:string;begin jlcd := strtoint(copy(m,1,5)); //提取记录长度,并转换为数值型 qsdz := strtoint(copy(m,13,5)); //提取数据字段区起始地址,并转换为数值型 zds := (qsdz-24) div 12; //字段个数 mcq := copy(m,25,qsdz - 24 - 1); //地址目次区字符串 sjq := copy(m,qsdz,jlcd - qsdz + 1); //数据字段区字符串 x := 1; jsq := 1; while jsq <= zds do //当jsq还小于字段的个数时,循环 begin zdh := copy(mcq,x,3); // 提取字段号 zdcd := strtoint(copy(mcq,x + 3,4));//字段长度 zddz := strtoint(copy(mcq,x + 7,5)); //字段起始地址 if zdh = a then //如果字段号等于指定的标识 begin exit; end else begin jsq := jsq + 1; x := x + 12; end; end; if jsq > zds then begin result := '';//无该字段,则返回空的字符串 exit; end; if (zdh = '001') or (zdh = '005') then begin result := copy(sjq,zddz + 2,zdcd); end; if subfid = chr(30) then //将数值型转换为字符型,并判断是否是字段分隔符 begin result := zzd_string; //返回指定字段的字符串 end; wz1 := pos(subfid,zzd_string); //取得子字段的起始位置 if wz1 < 1 then //当wz1=0时,即无该子字段 begin result :='';//无该子字段,则返回空字符串 end; string1 := copy(zzd_string,wz1 + 1,zdcd - 4); if (ord(string1[1]) = 31) or (ord(string1[1]) = 30) then //将截取到的字符转换为ASCII码,并判断 begin result := '';//该子字段为空串,则返回空串 exit; end; wz2 := pos(chr(31),string1);//将子字段分隔符ASCII值=31转换为对应的字符,并返回该字符在string1中的位置 if wz2 < 1 then //如果没有 begin wz2 := pos(chr(30),string1);//字段分隔符ASCII值=30 end; result := copy(string1,1,wz2 - 1); exit;end;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);var str:string; mfile:TextFile; r:string; // zd1:string; //zd2:string;begin AssignFile(mfile,'C:/1.txt');//打开才C盘中的文本文件 reset(mfile); while not eof(mfile) do begin readln(mfile,str); memo1.Lines.Add(str); end; closefile(mfile); r := memo1.Lines.Text; //在memo中一条marc记录赋给 r memo2.Lines.Add(marcsplit(r,'010','$a')); //调用函数end;end.-------------------------------------------------按F7进行单步调试时,总是在下面这段代码循环,跳不出去。while jsq <= zds do //当jsq还小于字段的个数时,循环 begin zdh := copy(mcq,x,3); // 提取字段号 zdcd := strtoint(copy(mcq,x + 3,4));//字段长度 zddz := strtoint(copy(mcq,x + 7,5)); //字段起始地址 if zdh = a then //如果字段号等于指定的标识 begin exit; end else begin jsq := jsq + 1; x := x + 12; end; end;
 
把a改成你要的标识,再加上一句如 if zdh = '005' then //如果字段号等于指定的标识 begin Result:=Copy(m,zddz,zdcd); exit; end else begin jsq := jsq + 1; x := x + 12; end;我试过没有问题的可以加进去,你试试
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
638
import
I
顶部