全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1740
推到 Plurk!
推到 Facebook!

如何去讀有簡體字的txt 到TStringGrid

 
joana
一般會員


發表:35
回覆:70
積分:21
註冊:2005-09-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-10-01 21:10:00 IP:61.230.xxx.xxx 訂閱

txt檔內容如下
A001#当大多数80后还处于懵懂和幻想阶段的时候#112臺北市公館路60號5樓#02-23241156#北區
A002#幻想阶段的时候#806高雄市大安路一段22號#07-7644321#南區
A003#当大多数80后还处于#100新竹市中山北路二段115號11樓之#302-23445731#北區

程式碼如下:執行後的StringGrid資料都變亂碼,怎麼辦?

拜託指導

procedure TForm1.BitBtn6Click(Sender: TObject);
var
i, count : Integer;
F : textFile ;
str : widestring;
ans : array[1..5] of string;
row : Integer;
begin
StringGrid1.RowCount := 2;
StringGrid1.Cells[0,0] := '編號';
StringGrid1.ColWidths[0] := 50;
StringGrid1.Cells[1,0] := '公司名稱';
StringGrid1.ColWidths[1] := 100;
StringGrid1.Cells[2,0] := '地址';
StringGrid1.ColWidths[2] := 250;
StringGrid1.Cells[3,0] := '電話';
StringGrid1.ColWidths[3] := 100;
StringGrid1.Cells[4,0] := '地區';
StringGrid1.ColWidths[0] := 40;
//
if FileExists(datafile) then
begin
AssignFile(F,datafile);
Reset(F);
row := 1;
try
while not EOF(F) do
begin
Readln(F,str); ----->程式執行到此 str這個變數已是亂碼

count := 1;
for i := 1 to 5 do ans[i] := '';
for i := 1 to length(str) do
if str[i] <> '#' then
ans[count] := ans[count] str[i]
else
count := count 1 ;
StringGrid1.Cells[0,row] := ans[1];
StringGrid1.Cells[1,row] := ans[2];
StringGrid1.Cells[2,row] := ans[3];
StringGrid1.Cells[3,row] := ans[4];
StringGrid1.Cells[4,row] := ans[5];
Inc(row);
StringGrid1.RowCount := row;
end;
finally
CloseFile(F);
end;
end;
end;

jhlin88
一般會員


發表:21
回覆:28
積分:9
註冊:2002-10-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-10-04 11:11:53 IP:219.87.xxx.xxx 未訂閱

我想是跟編碼有關係(假設你的作業系統是繁體的,那在delphi畫面中,如果用預設的字集,將無法顯示GB碼的字)

建議去參考一下unicode 補完計劃(google搜尋就找得到),安裝之後,也許馬上就能解決您的問題。

或是去找一下GB轉BIG5的工具或是函數,先轉換編碼,再顯示到畫面上。

以上是小弟個人淺見,若有錯誤請指教。


===================引 用 文 章===================

txt檔內容如下
A001#当大多数80后还处于懵懂和幻想阶段的时候#112臺北市公館路60號5樓#02-23241156#北區
A002#幻想阶段的时候#806高雄市大安路一段22號#07-7644321#南區
A003#当大多数80后还处于#100新竹市中山北路二段115號11樓之#302-23445731#北區

程式碼如下:執行後的StringGrid資料都變亂碼,怎麼辦?

拜託指導

procedure TForm1.BitBtn6Click(Sender: TObject);
var
i, count : Integer;
F : textFile ;
str : widestring;
ans : array[1..5] of string;
row : Integer;
begin
StringGrid1.RowCount := 2;
StringGrid1.Cells[0,0] := '編號';
StringGrid1.ColWidths[0] := 50;
StringGrid1.Cells[1,0] := '公司名稱';
StringGrid1.ColWidths[1] := 100;
StringGrid1.Cells[2,0] := '地址';
StringGrid1.ColWidths[2] := 250;
StringGrid1.Cells[3,0] := '電話';
StringGrid1.ColWidths[3] := 100;
StringGrid1.Cells[4,0] := '地區';
StringGrid1.ColWidths[0] := 40;
//
if FileExists(datafile) then
begin
AssignFile(F,datafile);
Reset(F);
row := 1;
try
while not EOF(F) do
begin
Readln(F,str); ----->程式執行到此 str這個變數已是亂碼

count := 1;
for i := 1 to 5 do ans[i] := '';
for i := 1 to length(str) do
if str[i] <> '#' then
ans[count] := ans[count] str[i]
else
count := count 1 ;
StringGrid1.Cells[0,row] := ans[1];
StringGrid1.Cells[1,row] := ans[2];
StringGrid1.Cells[2,row] := ans[3];
StringGrid1.Cells[3,row] := ans[4];
StringGrid1.Cells[4,row] := ans[5];
Inc(row);
StringGrid1.RowCount := row;
end;
finally
CloseFile(F);
end;
end;
end;

------
JamesLin
joana
一般會員


發表:35
回覆:70
積分:21
註冊:2005-09-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2006-10-04 12:00:45 IP:61.230.xxx.xxx 訂閱

HI,謝謝您的回覆! 但問題是程式執行到 Readln(F,str),我把 str這個變數叫出來看,已是亂碼,即使找到 GB ->BIG5之工具,也得str這個變數能正確抓到原始之資料,再來用 GB->BIG5之工具才有用.不曉得我的想法對不對?

try
while not EOF(F) do
begin
Readln(F,str); ----->程式執行到此 str這個變數已是亂碼

carsin
一般會員


發表:1
回覆:3
積分:5
註冊:2004-04-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2006-10-11 22:17:18 IP:218.162.xxx.xxx 未訂閱
系統時間:2024-07-01 14:54:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!