Delphi World - это проект, являющийся сборником статей и малодокументированных возможностей  по программированию в среде Delphi. Здесь вы найдёте работы по следующим категориям: delphi, delfi, borland, bds, дельфи, делфи, дэльфи, дэлфи, programming, example, программирование, исходные коды, code, исходники, source, sources, сорцы, сорсы, soft, programs, программы, and, how, delphiworld, базы данных, графика, игры, интернет, сети, компоненты, классы, мультимедиа, ос, железо, программа, интерфейс, рабочий стол, синтаксис, технологии, файловая система...
TWriter и TReader - читаем и пишем в файл



unit MsForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ButtonReadText: TButton;
    ButtonWriteLines: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Memo1: TMemo;
    ButtonReadLines: TButton;
    ButtonWriteRev: TButton;
    ButtonMoveLines: TButton;
    procedure ButtonReadTextClick(Sender: TObject);
    procedure ButtonWriteLinesClick(Sender: TObject);
    procedure ButtonReadLinesClick(Sender: TObject);
    procedure ButtonWriteRevClick(Sender: TObject);
    procedure ButtonMoveLinesClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ButtonReadTextClick(Sender: TObject);
var
  Str1: TFileStream;
  I: Integer;
begin
  OpenDialog1.Filter :=
    'Text File (*.txt)|*.txt|Pascal File (*.pas)|*.pas|' +
    'ReadWrite File (*.rwt)|*.rwt|Any file (*.*)|*.*';
  OpenDialog1.DefaultExt := 'txt';
  if OpenDialog1.Execute then
  begin
    Str1 := TFileStream.Create (
      OpenDialog1.Filename, fmOpenRead);
    try
      Memo1.Lines.LoadFromStream (Str1);
    finally
      Str1.Free;
    end;
    // enable all buttons
    for I := 0 to ControlCount - 1 do
      if Controls [I] is TButton then
        TButton (Controls [I]).Enabled := True;
  end;
end;


procedure TForm1.ButtonWriteLinesClick(Sender: TObject);
var
  Str1: TFileStream;
  Writer1: TWriter;
  I: Integer;
begin
  SaveDialog1.Filter :=
    'Read Write file (*.rwf)|*.rwf|Any file (*.*)|*.*';
  SaveDialog1.FilterIndex := 1;
  SaveDialog1.DefaultExt := 'rwt';
  if SaveDialog1.Execute then
  begin
    Str1 := TFileStream.Create (
      SaveDialog1.FileName, fmCreate or fmOpenWrite);
    Writer1 := TWriter.Create (Str1, 1024);
    try
      Writer1.WriteListBegin;
      for I := 0 to Memo1.Lines.Count - 1 do
        Writer1.WriteString (Memo1.Lines [I]);
      Writer1.WriteListEnd;
    finally
      Writer1.Free;
      Str1.Free;
    end;
  end;
end;

procedure TForm1.ButtonReadLinesClick(Sender: TObject);
var
  Str1: TFileStream;
  Reader1: TReader;
  I: Integer;
begin
  OpenDialog1.Filter :=
    'Read Write file (*.rwf)|*.rwf|Any file (*.*)|*.*';
  OpenDialog1.FilterIndex := 1;    
  OpenDialog1.DefaultExt := 'rwt';
  if OpenDialog1.Execute then
  begin
    Str1 := TFileStream.Create (
      OpenDialog1.Filename, fmOpenRead);
    Reader1 := TReader.Create (Str1, 1024);
    try
      Reader1.ReadListBegin;
      Memo1.Lines.Clear;
      while not Reader1.EndOfList do
      begin
        Memo1.Lines.Add (Reader1.ReadString);
        Application.ProcessMessages;
      end;
      Reader1.ReadListEnd;
    finally
      Reader1.Free;
      Str1.Free;
    end;
    // enable all buttons
    for I := 0 to ControlCount - 1 do
      if Controls [I] is TButton then
        TButton (Controls [I]).Enabled := True;
   end;
end;

procedure TForm1.ButtonWriteRevClick(Sender: TObject);
var
  Str1: TFileStream;
  Writer1: TWriter;
  I: Integer;
begin
  SaveDialog1.Filter :=
    'Read Write Test file (*.rwt)|*.rwt|Any file (*.*)';
  SaveDialog1.DefaultExt := 'rwt';
  if SaveDialog1.Execute then
  begin
    Str1 := TFileStream.Create (
      SaveDialog1.FileName, fmCreate or fmOpenWrite);
    Writer1 := TWriter.Create (Str1, 1024);
    try
      Writer1.WriteListBegin;
      for I := Memo1.Lines.Count - 1 downto 0 do
        Writer1.WriteString (Memo1.Lines [I]);
      Writer1.WriteListEnd;
    finally
      Writer1.Free;
      Str1.Free;
    end;
  end;
end;

procedure TForm1.ButtonMoveLinesClick(Sender: TObject);
var
  MemStr1: TMemoryStream;
  Writer1: TWriter;
  Reader1: TReader;
  I: Integer;
begin
  MemStr1 := TMemoryStream.Create;
  try
    Writer1 := TWriter.Create (MemStr1, 1024);
    try
      Writer1.WriteListBegin;
      Randomize;
      while Memo1.Lines.Count > 0 do
      begin
        I := Random (Memo1.Lines.Count);
        Writer1.WriteString (Memo1.Lines [I]);
        Memo1.Lines.Delete (I);
        Application.ProcessMessages;
      end;
      Writer1.WriteListEnd;
    finally
      Writer1.Free;
    end;
    // reset, rewind, move back
    // to the beginning of the stream
    MemStr1.Seek (0, soFromBeginning);
    Reader1 := TReader.Create (MemStr1, 1024);
    try
      Reader1.ReadListBegin;
      while not Reader1.EndOfList do
      begin
        Memo1.Lines.Add (Reader1.ReadString);
        Application.ProcessMessages;
      end;
      Reader1.ReadListEnd;
      finally
        Reader1.Free;
      end;
    finally
      memStr1.Free;
  end;
end;

end.

Загрузить весь проект

Проект Delphi World © Выпуск 2002 - 2004
Автор проекта: ___Nikolay