unit ReadDat;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
edt1: TEdit;
Button1: TButton;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//写入结构体
type
TCustomer = record
CenX: Integer;
end;
const cDivWorld=10000000;
procedure TForm1.Button1Click(Sender: TObject);
var
mmo1:TMemo;
i: Integer;
f1:Textfile;
myText: file of TCustomer; //file of type type必须是固定大小的,不能是对象, String, Variant等
customer: TCustomer;
S1:string;
S:String;
StrList1:TStringList;
LonLat:Double;
C: Cardinal;
begin
StrList1 := TStringList.Create;
try
S1:=edt1.Text;
C := GetFileAttributes(Pchar(S1));//把string转换为PAnsiChar
if C = $FFFFFFFF then
begin
ShowMessage('文件或文件夹不存在!');
edt1.text:='';
Exit;
end
else if C and FILE_ATTRIBUTE_DIRECTORY <> 0 then
begin
ShowMessage('是文件夹不是文件!后缀必须为.dat');
Exit;
end
else //正确打开文件
AssignFile(myText, S1); //要读取的dat文件
Reset(myText); //只读模式打开文件
while not Eof(myText) do
begin
read(myText,customer); //读取一个结构,不同的.dat文件中其含有的基本结构体不同,需要自己去调整
LonLat:=customer.CenX / cDivWorld;
S1:=Format('%g',[LonLat]);
StrList1.Add(S1); //把数据写入文本文件中
end;
StrList1.SaveToFile('C:\Users\trt103\Desktop\456.txt'); //将数据写入到.txt文本中
StrList1.Clear; {清空}
StrList1.Free; {释放}
CloseFile(myText);
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
Exit;
end;
end
end;
end.