如何遍历数组并写到文件

Rudeeeeeee 2008-06-26 02:35:03
如何遍历数组并写到文件

gcCitys: array[gtCityRange] of TCityInformation = (
(ID:40000; Name:'朔州'; Code:1406; TelCode:349),
(ID:40001; Name:'石家庄'; Code:1301; TelCode:311),
(ID:40002; Name:'保定'; Code:1306; TelCode:312))

写入文件格式:
40000,朔州,1406,349
40001,石家庄,1301,311

...全文
101 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
喝口水 2008-06-26
  • 打赏
  • 举报
回复
sa
喝口水 2008-06-26
  • 打赏
  • 举报
回复
type
TCityInformation=record
ID:integer;
Name:string;
Code:integer;
TelCode:integer;
end;

var
Form1: TForm1;
gcCitys: array[1..3] of TCityInformation = (//gtCityRange不知它是什么东西,暂且用1..3代替了
(ID:40000; Name:'朔州'; Code:1406; TelCode:349),
(ID:40001; Name:'石家庄'; Code:1301; TelCode:311),
(ID:40002; Name:'保定'; Code:1306; TelCode:312));
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
CityList:TStringlist;
str:string;
i:integer;
begin
citylist:=TStringlist.Create;
for i:=low(gcCitys) to high(gcCitys) do
begin
str:=inttostr(gccitys[i].ID)+','+gccitys[i].Name+','+inttostr(gccitys[i].Code)+','+inttostr(gccitys[i].TelCode);
citylist.Add(str);
end;
citylist.SaveToFile('1.txt');//保存到文件
citylist.Free;
end;
老之 2008-06-26
  • 打赏
  • 举报
回复
参考一下:


type
TCityInformation = packed record
ID : Integer;
Name: string;
Code: Integer;
TelCode: Integer;
end;

gtCityRange = 0..2;
const
gcCitys: array[gtCityRange] of TCityInformation = (
(ID:40000; Name:'朔州'; Code:1406; TelCode:349),
(ID:40001; Name:'石家庄'; Code:1301; TelCode:311),
(ID:40002; Name:'保定'; Code:1306; TelCode:312));
var
sl: TStringList;
i: Integer; //可能是枚举类型
begin
sl := TStringList.Create;
for i := Low(gtCityRange) to High(gtCityRange) do
begin
sl.Add(Format('%d,%s,%d,%d',[gcCitys[i].Id, gcCitys[i].Name, gcCitys[i].Code, gcCitys[i].TelCode ]));

end;
sl.SaveToFile('temp.txt');
sl.Free;
end;

2,496

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧