我將陣列變數存檔案,但再讀回來其中第二、四組變數全為0。
procedure TForm1.Button1Click(Sender: TObject);
var
MF: TStringList;
begin
MF:= TStringList.Create;
try
For i:=1 to 20 do
MF.Add(inttostr(wjpx0[i]));
For i:=1 to 20 do
MF.Add(inttostr(xjpx0[i]));
For i:=1 to 20 do
MF.Add(inttostr(sjpx0[i]));
For i:=1 to 6 do
MF.Add(inttostr(dppx0[i]));
MF.SaveToFile('abc'+'.txt');
finally
MF.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MF: TStrings;
begin
If Opendialog1.Execute then begin
MF:= TStringList.Create;
try
MF.LoadFromFile(opendialog1.filename);
//icount:=MF.Count;
For i:=0 to 64 do begin
j:=strtoint(MF.strings[i]);
If i in [0..19] then
wjpx0[i+1]:=j
else if i in [20..39] then
xjpx0[i+1]:=j
else if i in [40..59] then
sjpx0[i+1]:=j
else if i in [60..64] then
end;
finally
MF.Free;
end; end;
end;
這樣的陳述句用過千次,這次問題怎么跟蹤也找不到問題出在哪里?
百思不得其解,請幫我看一下。
uj5u.com熱心網友回復:
跟蹤發現,應該賦值給第二組陣列變數的值全部賦給第三組變數。我換了臺電腦測驗,問題依舊。不是系統問題,應該還是代碼問題。
uj5u.com熱心網友回復:
找開檔案看看不就知道了,另外你的陣列是怎么定義的?保存時用的陣列下標是1..20
后面讀取賦值,比如xjpx0,是21..40,不會越界嗎?
uj5u.com熱心網友回復:
頂樓上uj5u.com熱心網友回復:
varForm1: TForm1;
dppx0 : array[1..6] of Integer;
wjpx0,xjpx0,sjpx0 : array[1..20] of Integer;
i,j,aa:integer;
uj5u.com熱心網友回復:
汗。。所以你越界了uj5u.com熱心網友回復:
2樓說的還真有理,但我改成procedure TForm1.Button2Click(Sender: TObject);
var
MF: TStrings;
begin
If Opendialog1.Execute then begin
MF:= TStringList.Create;
try
MF.LoadFromFile(opendialog1.filename);
//icount:=MF.Count;
For i:=0 to 64 do begin
j:=strtoint(MF.strings[i]);
If i in [0..19] then
wjpx0[i+1]:=j
else if i in [20..39] then
xjpx0[i+1-20]:=j
else if i in [40..59] then
sjpx0[i+1-40]:=j
else if i in [60..64] then
sjpx0[i+1-60]:=j
end;
finally
MF.Free;
end; end;
end;
還不行!
uj5u.com熱心網友回復:
為什么你MF定義為TStrings而用TStringList.Create?uj5u.com熱心網友回復:
回樓上,TStrings有虛函式,所以用子類TStringList.Createuj5u.com熱心網友回復:
其中關鍵部分換成這個試試。For i:=0 to 64 do begin
j:=strtoint(MF.strings[i]);
case i of
0..19: wjpx0[i+1]:=j;
20..39: xjpx0[i+1-20]:=j;
40..59: sjpx0[i+1-40]:=j;
60..64: dppx0[i+1-60]:=j;
end;
end;
uj5u.com熱心網友回復:
既然你用TStringList.Create,不如直接將它定義成TStringList。還有你打開檔案看過了嗎?資料真的對嗎?問題不一定只出在讀取部分uj5u.com熱心網友回復:
9樓的表達方式就明了、清晰多了。要越界,就都越了,既然保存時用了 MF.SaveToFile('abc'+'.txt'); 讀的時候就直接 MF.LoadFromFile('abc'+'.txt'); 好了。別是弄暈了,用那對話選擇檔案時選錯了。
uj5u.com熱心網友回復:
LZ,你查看過檔案了嗎uj5u.com熱心網友回復:
寫入檔案前,數字之間,要加分隔符如逗號,空格什么的,換行。讀回來時,通過分隔符區別不同資料,并濾除分隔符。
uj5u.com熱心網友回復:
不然兩個數字,會合并成一個,你會丟失資料轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/133617.html
標籤:語言基礎/算法/系統設計
上一篇:求演算法, 任意位置大小的兩個圓過圓心直徑確定的四邊形四個頂點坐標
下一篇:非線性方程組的問題
