zoukankan      html  css  js  c++  java
  • Delphi初浅入门笔记之七:文件操作

    Delphi中的文件分为文本文件、有类型文件和无类型文件。在一般情况下,文件仅指磁盘文件,外设如打印机、显示器也是文件,这里的文件仅指磁盘文件。

    文件的基本操作:

    与外部文件联系的建立与中断

    在Delphi中要对外部为难进行读写操作前后,需要将该外部文件名分配给一个文件类型的变量;当不需要对外部文件进行读写时,需要中断文件变量与该外部磁盘文件的联系。

    文件的打开与关闭

    在对文件进行读写操作前后要打开或者关闭该文件。

    关于文件的操作就只了解了这么多。最后用一个简单的小例子来说明吧:

    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ToolWin, ActnMan, ActnCtrls, ActnMenus, Menus,
      ComCtrls;

    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        MainMenu1: TMainMenu;
        F1: TMenuItem;
        N1: TMenuItem;
        O1: TMenuItem;
        S1: TMenuItem;
        E1: TMenuItem;
        E2: TMenuItem;
        RichEdit1: TRichEdit;
        OpenDialog1: TOpenDialog;
        SaveDialog1: TSaveDialog;
        ColorDialog1: TColorDialog;
        procedure FormCreate(Sender: TObject);
        procedure N1Click(Sender: TObject);
        procedure O1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;
      f:textfile;
      wfilename:string;
      flag:Boolean;

    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Memo1.Text:='Delphi对外部文件操作前后,需要将该外部文件分配给'+
      '一个文件类型的的变量;当不需要对外部文件进行读写时,需要中断文件变量与该'+
      '外部磁盘文件的联系';
    end;

    procedure TForm1.N1Click(Sender: TObject);
    begin
    assignfile(f,'C:\123.txt');//与C:\123.txt的文件建立联系
    form1.Caption:='123.txt';//将标题栏更改为文件的名字
    rewrite(f); //Rewrite函数是以写的方式打开文件

    end;

    procedure TForm1.O1Click(Sender: TObject);
    var
    line:string;
    begin
        if opendialog1.Execute  then
        begin
            assignfile(f,opendialog1.filename);
            richedit1.Enabled:=false;
        end;

        while not eof(f) do
        begin
            readln(f,line);
            richedit1.Lines.Add(line);
        end;
        closefile(f);
    end;

    end.

    源代码

  • 相关阅读:
    Leetcode 92. Reverse Linked List II
    Leetcode 206. Reverse Linked List
    Leetcode 763. Partition Labels
    Leetcode 746. Min Cost Climbing Stairs
    Leetcode 759. Employee Free Time
    Leetcode 763. Partition Labels
    搭建数据仓库第09篇:物理建模
    Python进阶篇:Socket多线程
    Python进阶篇:文件系统的操作
    搭建数据仓库第08篇:逻辑建模–5–维度建模核心之一致性维度2
  • 原文地址:https://www.cnblogs.com/liszt/p/1969446.html
Copyright © 2011-2022 走看看