Delphi怎么判断一个文件存在与否,文件还是文件夹?


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.
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值