數值的資料load出來時是空值 |
答題得分者是:st33chen
|
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
我有一個全是數值的文字檔案
其格式是: [00:01] [00:47].... 然而當我在使用了pos和copy來取出當中的數字(00和01)時 在第一筆資料有出現我要的資料 可是在第二筆時就出現了空值 請問是什麼原因導致的呢?? 是儲存檔案格式的問題嗎? 以下附上取出數值的原始碼~ [code delphi] function strisint(str: string): boolean; begin try strtoint(str); Result := true; except Result := false; end; end; /////////////////// procedure TForm1.readfile; if FileExists(filename) then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile(filename); for i := 0 to sl.Count - 1 do begin y := pos('[', sl.strings[i]); temp := sl.strings[i]; tmp := TStringList.Create; while y <> 0 do begin sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin end; finally end; end; end; end; [/code] 當中是錯在if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then上 文字檔: http://delphi.ktop.com.tw/board.php?cid=31&fid=97&tid=96201 |
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
Hi, 試試
procedure TForm1.readfile; if FileExists(filename) then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile(filename); for i := 0 to sl.Count - 1 do begin y := pos('[', sl.strings[i]); temp := sl.strings[i]; tmp := TStringList.Create; while y <> 0 do begin sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strtointdef(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin end; end; // while end; // for finally end; end; ===================引 用 老大仔 文 章=================== 我有一個全是數值的文字檔案 其格式是: [00:01] [00:47].... 然而當我在使用了pos和copy來取出當中的數字(00和01)時 在第一筆資料有出現我要的資料 可是在第二筆時就出現了空值 請問是什麼原因導致的呢?? 是儲存檔案格式的問題嗎? 以下附上取出數值的原始碼~ [code delphi] function strisint(str: string): boolean; begin try strtoint(str); Result := true; except Result := false; end; end; /////////////////// procedure TForm1.readfile; if FileExists(filename) then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile(filename); for i := 0 to sl.Count - 1 do begin y := pos('[', sl.strings[i]); temp := sl.strings[i]; tmp := TStringList.Create; while y <> 0 do begin sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin end; finally end; end; end; end; [/code] 當中是錯在if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then上 文字檔: http://delphi.ktop.com.tw/board.php?cid=31&fid=97&tid=96201
------
What do we live for if not to make life less difficult for each other? |
st33chen
尊榮會員 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
您好, 雖然要處理這程式有別的方法, 但仍然用您的邏輯來和您討論. 我發覺原程式在巢狀結構上怪怪的, 會造成巢與巢交叉的現象.
我整理了一下, 改成如下 : if FileExists('c:\test\新增文字文件.txt') then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile('c:\test\新增文字文件.txt'); for i := 0 to sl.Count - 1 do begin y := pos('[', sl.strings[i]); temp := sl.strings[i]; tmp := TStringList.Create; <--- 一直 create, 又沒用到, 照理第二次 create 時就會出錯, 但您程式這一句只走一次 while y <> 0 do begin <--- y 在這個 while 裏沒變過, 造成無限迴路 sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin end; end; end; finally end; end; 紅字的部份是我發現不對的地方, 我實測的結果發現 y 在 while y<>0 do 內沒改變過, 一直跳不出來 ----------------------------------------------------- 我用您的邏輯改寫了一下: if FileExists('c:\test\新增文字文件.txt') then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile('c:\test\新增文字文件.txt'); for i := 0 to sl.Count - 1 do begin temp := sl.strings[i]; // 對調 y := pos('[', temp); // 對調 // tmp := TStringList.Create; 註掉 while y <> 0 do begin sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin // do something end; y := pos('[', temp); // 新增 end; end; finally // 往下調到這 end; end;
------
IS IT WHAT IT IS 我是 李慕白 請倒著唸. 又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦); 都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲. |
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
糟糕~剛剛發現後段有一小段程式碼未po上
我現在補上: (即為st33chen大大最後一次貼上的那段程式碼) procedure TForm1.readfile; if FileExists(filename) then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile(filename); for i := 0 to sl.Count - 1 do begin y := pos('[', sl.strings[i]); temp := sl.strings[i]; tmp := TStringList.Create; while y <> 0 do begin sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin end; y := pos('[', temp); end; tmp.Free; finally end; end; end; 請問st33chen~ 那麼假如在不考慮其他狀況下 在執行if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then這行時 我有用showmessage來檢查一下 發現第一次確定是出現00、01這兩個數字 但是當它跑第二次的時候 卻是空的(一個東西也都沒顯示出來) 請問這是為什麼呢?(所以會在function strisint中就卡住了) |
st33chen
尊榮會員 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
您的程式還是會有巢狀結構交叉 的情形, 照理連 compile 都不過的. 我再稍改您的程式, 確定可以正確執行, 請您參考比對一下 (這個 project, 我只拉一個 tbutton, 其 click 事件就是您程式的部份) unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function strisint(str: string): boolean; begin try strtoint(str); Result := true; except Result := false; end; end; procedure TForm1.Button1Click(Sender: TObject); var sl, sll, tmp : tstringlist; i, y : integer; temp, sj : string; begin if FileExists('c:\test\新增文字文件.txt') then begin sl := TStringList.Create; sll := TStringList.Create;; try sl.LoadFromFile('c:\test\新增文字文件.txt'); for i := 0 to sl.Count - 1 do begin y := pos('[', sl.strings[i]); temp := sl.strings[i]; tmp := TStringList.Create; while y <> 0 do begin sj := copy(temp, pos('[', temp) 1, pos(']', temp) - pos('[', temp) - 1); temp := copy(temp, pos(']', temp) 1, length(temp) - pos(']', temp)); if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then begin showmessage(copy(sj, 1, 2)); // 新增測試用 showmessage(copy(sj, 4, 2)); // 新增測試用 end; y := pos('[', temp); end; tmp.Free; end; finally // 修改到正確位置 end; end; end; end.
------
IS IT WHAT IT IS 我是 李慕白 請倒著唸. 又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦); 都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲. |
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
大大您的程式確實是可以執行
可是它在執行完第一筆資料後 在執行第二筆資料時 卻停住了,會有錯誤 程式會卡在if strisint(copy(sj, 1, 2)) and strisint(copy(sj, 4, 2)) then 因為第二筆抓出來的資料變成空值了 可是我的文字檔明明還有資料 為什麼抓出來的資料會是空的呢? 我有在最上面附上我的文字檔(或範例區那邊也有) http://delphi.ktop.com.tw/board.php?cid=31&fid=97&tid=96201 可以請您幫我看看嗎?
編輯記錄
老大仔 重新編輯於 2008-11-27 13:55:39, 註解 無‧
|
st33chen
尊榮會員 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
|
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |