zoukankan      html  css  js  c++  java
  • 处理之文本文件

     1 unit Unit1;
     2 
     3 interface
     4 
     5 uses
     6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     7   Dialogs, StdCtrls;
     8 
     9 type
    10   TForm1 = class(TForm)
    11     Button1: TButton;
    12     Button2: TButton;
    13     Button3: TButton;
    14     procedure Button1Click(Sender: TObject);
    15     procedure Button2Click(Sender: TObject);
    16     procedure Button3Click(Sender: TObject);
    17   private
    18     { Private declarations }
    19   public
    20     { Public declarations }
    21   end;
    22 
    23 var
    24   Form1: TForm1;
    25 
    26 implementation
    27 
    28 var
    29   F: TextFile;
    30   FileName: string = 'c:/A.txt';
    31 
    32 
    33 {$R *.dfm}
    34 
    35 //向文本文件写入内容
    36 procedure TForm1.Button1Click(Sender: TObject);
    37 begin
    38   AssignFile(F,FileName);
    39   Rewrite(F);        //重写(覆盖)已存在的文件
    40   Writeln(F,'one');  //写入一行
    41   Writeln(F,'two');
    42   CloseFile(F);
    43 end;
    44 
    45 //读取文本文件内容
    46 procedure TForm1.Button2Click(Sender: TObject);
    47 var
    48   str:string;
    49 begin
    50   AssignFile(F,FileName);
    51   Reset(F);         //以只读方式打开文本文件
    52   while not Eof(F) do      //循环文本文件,判断是否到尾部
    53   begin
    54     Readln(F,str);         //读取一行
    55     ShowMessage(str);
    56   end;
    57 end;
    58 
    59 //向文本文件追加内容
    60 procedure TForm1.Button3Click(Sender: TObject);
    61 begin
    62   AssignFile(F,FileName);
    63   Append(F);              //打开文件准备追加,指针指向文本末尾
    64   Writeln(F, 'three');   //写入一行
    65   Writeln(F, 'four');
    66   CloseFile(F);
    67 end;
    68 
    69 end.
    View Code
  • 相关阅读:
    Python生成器
    Python迭代器
    模块
    列表推倒式
    内置函数 lambda表达式
    函数
    global、nonlocal、迭代器
    练习
    编码数组字典
    字典
  • 原文地址:https://www.cnblogs.com/key-ok/p/3428850.html
Copyright © 2011-2022 走看看