您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> 如何遍历数组并写到文件

如何遍历数组并写到文件

来源:网络整理     时间:2016/8/10 20:07:31     关键词:

关于网友提出的“ 如何遍历数组并写到文件”问题疑问,本网通过在网上对“ 如何遍历数组并写到文件”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 如何遍历数组并写到文件
描述:

如何遍历数组并写到文件
  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


解决方案1:

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;

解决方案2:

参考一下:


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;

以上介绍了“ 如何遍历数组并写到文件”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3196697.html

相关图片

相关文章