如何在deplhi中使用此过程获取两个不同的文件 [英] how to get two different file with this procedure in deplhi
问题描述
我想从两个文件.txt中获取价值,一个文件与另一个文件包含不同的维度矩阵
i want to get value from two file .txt, one file contain different dimension matrix with other
我尝试过以下代码:
procedure TfrmJST.ParseDelimited(const S1: TStrings; const Value: String; const Delimiter: String);
var
dx,cx: integer;
ns,ms: String;
txt: string;
delta,teta: integer;
procedure TfrmJST.ParseDelimited(const S1: TStrings; const Value: String; const Delimiter: String);
var
dx,cx: integer;
ns,ms: String;
txt: string;
delta,teta: integer;
begin
Col := 1;
Delta := Length(Delimiter);
Txt := Value+Delimiter;;
begin
while Length(Txt) > 1 do
begin
Dx := Pos(Delimiter, Txt);
Ns := Trim(Copy(Txt, 1, Dx-1));
// S1.Add('#'+Ns+'*'); //only needed for testing
if Ns <> '' then
begin
Matrix[Row,Col] := StrToFloat(Ns); //for first matrix
Inc(Col);
end;
Txt := Copy(Txt, Dx+Delta, MaxInt);
end;
end;
Col := 1;
teta := Length(delimiter);
txt := value+delimiter;
begin
while Length(txt) > 1 do
begin
cx := Pos(delimiter, txt);
ms := Copy(txt, 1, cx-1);
if ms <> '' then
begin
ref[Row,Col] := StrToFloat(ms); ///for 2nd matrix
Inc(Col);
end;
txt := Copy(txt, cx+teta, MaxInt);
end;
end;
end;
这是矩阵的初始化:
private
{ Private declarations }
Row, Col: integer;
Matrix: array[1..140,1..141] of double;
Ref: array[1..2,1..140] of double ;
这是实现:
begin
Temp := TStringList.Create;
MemoSL:= TStringList.Create ;
Temp.LoadFromFile('trainer.txt');
Row := 1;
for I := 0 to Temp.Count-1 do
begin
ParseDelimited(MemoSL, Trim(Temp.Strings[I]), ' ');
Inc(Row); //stackoverflow error in this line
end;
Temp.Free;
//parsing second matrix
TempList := TStringList.Create;
Templist.LoadFromFile('refbaru.txt');
row := 1;
for J := 0 to Templist.Count-1 do
begin
T := Templist[J];
ParseDelimited(Memo1.Lines, T, ' ');
Inc(row);
end;
Templist.Free;
我尝试了该代码,但给了我错误, 错误是处理第一个矩阵的'inc(row)'行中的stackoverflow错误. 当我在处理第二个矩阵的第二个函数中给出注释时,Temp [i]仅返回2行矩阵[140x141].这是否意味着代码无法处理两个不同的文件?为什么只返回矩阵的两行? 有人可以帮助我吗?
i tried that code but give me error, the error was stackoverflow error in line 'inc(row)' that process first matrix. and while i gave comment out at the second function that process 2nd matrix, Temp[i] only returns 2 rows of matrix[140x141]. does it mean the code can't process two different file? and why it only return two rows of the matrix? anyone can help me?
推荐答案
在已经存在许多普通错误的情况下寻找特定的堆栈溢出错误是毫无意义的.
It is pointless to look for a specific stack overflow error when many ordinary errors already exist.
如果您的代码是干净编程的,但仍为stack overflow
,那么现在是时候深入了解代码了.
但首先 !只要您看到明显的错误,就应该将其删除.
If your code is clean programmed and it is still stack overflow
, then of course, is time to look deeper into the code.
But first ! As long as you can see obvious errors, you should remove them.
- 1.)行"在140维数组和仅2维数组上的相同过程中使用. 那怎么办?
- 1.) "Row" used in the same procedure on a 140 dimension array and on a only 2 dimension array. How can that work ?
矩阵:double的array [1..140,1..141];
引用:double的array [1..2,1..140];
文件'trainer.txt'140行
文件'refbaru.txt'2行.
Matrix: array[1..140,1..141] of double;
Ref: array[1..2,1..140] of double ;
File 'trainer.txt' 140 Lines
File 'refbaru.txt' 2 Lines.
for I := 0 to Temp.Count-1 do // 140 lines
// ParseDelimited() will only run properly if Row < 3
// remember -> Ref: array[1..2,1..140])
// if Row > 2 , with Ref[Row,Col] := , 137 times data is overwritten.
procedure ParseDelimited(MemoSL, Trim(Temp.Strings[I]), ' ');
....
Matrix[Row,Col] := StrToFloat(Ns);
....
Ref[Row,Col] := StrToFloat(ms);
....
end;
Inc(Row);
end;
- 2.)如果使用
refbaru.txt
运行第二个loop
,并且两个数组同时出现在过程ParseDelimited()
中,则将覆盖数组Matrix
的2个值 - 2.) If you run the second
loop
withrefbaru.txt
and the two arrays are present together in the procedureParseDelimited()
, then you overwrite 2 values of arrayMatrix
- 确保:遍历
trainer.txt
,仅将值写入Matrix array
. - 确保:遍历
refbaru.txt
,仅将值写入Ref array
. - make sure: Loop through
trainer.txt
, writes values only to theMatrix array
. - make sure: Loop through
refbaru.txt
, writes values only to theRef array
.
推荐
您的代码可能类似于:
[...]
filetoload: String;
[...]
procedure TfrmJST.ParseDelimited(S1: TStrings; Value: String; const Delimiter: String);
var
f:double;
[...]
Col := 1;
txt := Value+Delimiter;
[...]
if filetoload='trainer.txt' then begin
Delta := Length(Delimiter);
while Length(txt) > 1 do
begin
Dx := Pos(Delimiter, txt);
Ns := Trim(Copy(txt, 1, Dx-1));
if Ns <> '' then
begin
if TryStrToFloat(Ns,f) then Matrix[Row,Col]:=f;
Inc(Col);
if Col > MatrixColMax then break;
txt := Copy(txt, Dx+Delta, MaxInt);
end else txt:='';
end;
end;
if filetoload='refbaru.txt' then begin
teta := Length(delimiter);
while Length(txt) > 1 do
begin
cx := Pos(delimiter, txt);
ms := Copy(txt, 1, cx-1);
if ms <> '' then
begin
if TryStrToFloat(ms,f) then Ref[Row,Col]:=f;
Inc(Col);
if Col > RefColMax then break;
txt := Copy(txt, cx+teta, MaxInt);
end else txt:='';
end;
end;
begin
[...]
filetoload:='trainer.txt';
Temp := TStringList.Create;
Temp.LoadFromFile(filetoload);
if Temp.Count > MatrixRowMax then LinesToLoad:=MatrixRowMax-1 else
LinesToLoad:=Temp.Count-1;
for I := 0 to LinesToLoad do
[...]
ParseDelimited(MemoSL, Trim(Temp.Strings[I]), ' ');
[...]
end;
filetoload:='refbaru.txt';
TempList := TStringList.Create;
TempList.LoadFromFile(filetoload);
if TempList.Count > RefRowMax then LinesToLoad:=RefRowMax-1 else
LinesToLoad:=TempList.Count-1;
for J := 0 to LinesToLoad do
[...]
ParseDelimited(Memo1.Lines, T, ' ');
[...]
end;
end;
您还应该将文件的行大小与数组的大小进行比较
RefRowMax: integer;
RefColMax: integer;
MatrixRowMax: integer;
MatrixColMax: integer;
LinesToLoad: integer;
....
RefRowMax:=2;
RefColMax:=140;
MatrixRowMax:=140;
MatrixColMax:=141;
....
过程ParseDelimited()
procedure ParseDelimited()
if filetoload='trainer.txt' then begin
[...]
Inc(Col)
if Col > MatrixColMax then break;
end;
if filetoload='refbaru.txt' then begin
[...]
Inc(Col)
if Col > RefColMax then break;
end;
在写入ParseDelimited()中的数组之前,还应该查找有效值Ns
和StrToFloat(Ns)
You should also look for a valid value of Ns
, StrToFloat(Ns)
before you write to the arrays in ParseDelimited()
- 函数TryStrToFloat(const S:string; out Value:Double):布尔值;
或 - Val();
- function TryStrToFloat(const S: string; out Value: Double): Boolean;
or - Val();
var
f:double;
....
begin
....
if TryStrToFloat(Ns,f) then Matrix[Row,Col]:=f;
....
OP
会覆盖许多已使用的数据.
并且当他有足够的数据被覆盖时,他会得到一个堆栈溢出错误.
The OP
overwritting many of used data.
And when he has enough data overwritten, he gets a stack overflow error.
这篇关于如何在deplhi中使用此过程获取两个不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!