請教各位前輩一個文字檔編輯的問題 |
尚未結案
|
citygood123
一般會員 發表:6 回覆:6 積分:2 註冊:2005-05-29 發送簡訊給我 |
|
Stallion
版主 發表:52 回覆:1600 積分:1995 註冊:2004-09-15 發送簡訊給我 |
|
citygood123
一般會員 發表:6 回覆:6 積分:2 註冊:2005-05-29 發送簡訊給我 |
|
Stallion
版主 發表:52 回覆:1600 積分:1995 註冊:2004-09-15 發送簡訊給我 |
|
citygood123
一般會員 發表:6 回覆:6 積分:2 註冊:2005-05-29 發送簡訊給我 |
感謝Stallion的回答!!
最近重新思考這問題並試著寫了一小段,但仍有問題無法解決,因條件是寫入txt時其中的=及逗號為資料類型的分隔符號不能更動及刪除,也不能兩者都使用相同符號(既都使用=或,) 以下程式中當讀取時仍會將第三個欄位的資料讀在第二個欄位,有無方式巧可以將第三個欄位讀在正確欄位?麻煩知道的先進告知一下, 謝謝! nterface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids; type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm}
var modified : Boolean = False;
Const datafile : String = 'goods.txt'; procedure TForm1.FormCreate(Sender: TObject);
var
i, count : Integer;
F : textFile ;
str : string;
ans : array[1..3] of string;
row : Integer;
begin
StringGrid1.RowCount := 2;
StringGrid1.Cells[0,0] := '商品名稱';
StringGrid1.ColWidths[0] := 300;
StringGrid1.Cells[1,0] := '售價';
StringGrid1.ColWidths[1] := 200;
StringGrid1.Cells[2,0] := '供貨狀況';
StringGrid1.ColWidths[2] := 300;
//
if FileExists(datafile) then
begin
AssignFile(F,datafile);
Reset(F);
row := 1;
try
while not EOF(F) do
begin
Readln(F,str);
count := 1;
for i := 1 to 2 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];
Inc(row);
StringGrid1.RowCount := row;
end;
finally
CloseFile(F);
end;
end;
end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i, row : Integer;
F : textFile ;
str : string;
begin
if modified then
if MessageDlg('儲存?',mtConfirmation,
mbOKCancel,0) = mrOK then
Begin
Assignfile(F,datafile);
Rewrite(F);
try
for row := 1 to pred(StringGrid1.RowCount) do
Begin
str := '';
for i := 0 to 0 do
str := str StringGrid1.Cells[i,row] '=';
str := str StringGrid1.Cells[1,row];
for i := 1 to 1 do
str := str StringGrid1.Cells[i,row] ',';
writeln(F,str);
end;
finally
Closefile(F);
end;
end;
end; procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
begin
modified := True;
end; end.
|
goat
高階會員 發表:53 回覆:130 積分:134 註冊:2002-06-03 發送簡訊給我 |
我的做法請參考
來源使用TiniFile的方式讀入->塞入Table中處理(修改)->由Table寫回文字檔。 Delphi Help↓ The following example reads information from a file into three listboxes on a form. uses IniFiles;
procedure TForm1.FormActivate(Sender: TObject); var
AppIni: TIniFile;
begin
AppIni := TIniFile.Create('win.ini');
AppIni.ReadSections(ListBox1.Items);
AppIni.ReadSection('Ports',Listbox2.Items);
AppIni.ReadSectionValues('Ports',ListBox3.Items);
AppIni.Free;
end; 不過你好像貼錯地方了吧! 發表人 - goat 於 2005/06/28 08:57:31
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |