Board index » delphi » Read File and Write file?

Read File and Write file?

Hi
I wrote a application.During run time, I create a form and some component
on that form.
I wonder how to save that file like DFM format and read it?

Thanks
--
Harden ZHU
Harde...@WorldNet.att.net

 

Re:Read File and Write file?


In article <01bc2354$2fd5d860$57e893cf@default>,
   "Harden ZHU" <Harde...@Worldnet.att.net> wrote:

Quote
>I wrote a application.During run time, I create a form and some component
>on that form.
>I wonder how to save that file like DFM format and read it?

You can do some neat little tricks with TReaders and TWriters:

You can use code like this to write it:

var
  MyFile : TFileStream;
  MyWriter : TWriter;
begin
  MyFile := TFileStream.Create('MyFile',fmCreate);
  try
    MyWriter := TWriter.Create(MyFile,4096);
    try
      MyWriter.WriteRootComponent(MyNewComponentOnTheForm);
    finally
      MyWriter.Free;
    end;
  finally
    MyFile.Free;
  end;
end;

And code like this to read it:

var
  MyFile : TFileStream;
  MyWriter : TReader;
  MyNewComponent : TMyComponent;
begin
  MyFile := TFileStream.Create('MyFile',fmOpenRead);
  try
    MyReader := TReader.Create(MyFile,4096);
    try
      MyNewComponent := TMyNewComponent.Create(Self);
      MyNewComponent :=
        MyReader.ReadRootComponent(MyNewComponent);
    finally
      MyReader.Free;
    end;
  finally
    MyFile.Free;
  end;
end;

There are some other tricks with RegisterClasses that make it so you
don't have to create the component up front... but I'm not good with
that stuff off the top of my head :)

Take a gander at Ray Lischner's Delphi Secrets book if you're a bit
technically inclined - it has lots of good stuff, including the .DFM
format and how streams work.

  --=- Ritchie Annand

Re:Read File and Write file?


Hi
Does you have more details or examples about that?

Thanks

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
R.Annand <anna...@cadvision.com> wrote in article
<5f0sia$...@elmo.cadvision.com>...

Quote
> In article <01bc2354$2fd5d860$57e893cf@default>,
>    "Harden ZHU" <Harde...@Worldnet.att.net> wrote:
> >I wrote a application.During run time, I create a form and some
component
> >on that form.
> >I wonder how to save that file like DFM format and read it?

> You can do some neat little tricks with TReaders and TWriters:

> You can use code like this to write it:

> var
>   MyFile : TFileStream;
>   MyWriter : TWriter;
> begin
>   MyFile := TFileStream.Create('MyFile',fmCreate);
>   try
>     MyWriter := TWriter.Create(MyFile,4096);
>     try
>       MyWriter.WriteRootComponent(MyNewComponentOnTheForm);
>     finally
>       MyWriter.Free;
>     end;
>   finally
>     MyFile.Free;
>   end;
> end;

> And code like this to read it:

> var
>   MyFile : TFileStream;
>   MyWriter : TReader;
>   MyNewComponent : TMyComponent;
> begin
>   MyFile := TFileStream.Create('MyFile',fmOpenRead);
>   try
>     MyReader := TReader.Create(MyFile,4096);
>     try
>       MyNewComponent := TMyNewComponent.Create(Self);
>       MyNewComponent :=
>         MyReader.ReadRootComponent(MyNewComponent);
>     finally
>       MyReader.Free;
>     end;
>   finally
>     MyFile.Free;
>   end;
> end;

> There are some other tricks with RegisterClasses that make it so you
> don't have to create the component up front... but I'm not good with
> that stuff off the top of my head :)

> Take a gander at Ray Lischner's Delphi Secrets book if you're a bit
> technically inclined - it has lots of good stuff, including the .DFM
> format and how streams work.

>   --=- Ritchie Annand

Re:Read File and Write file?


Hi

Writing component to the file is fine.But when read component from file has
problem.
I must tell the program which component I will read like follow. If I don't
tell that then  the program will
give EClassNoFound error during running time.

        >       MyNewComponent :=
        >         MyReader.ReadRootComponent(MyNewComponent);

Problem is that I save all kind of components to the file during
writing.How can the program know which component is read during reading.
Any idea?

Thanks

Harden

Quote

> var
>   MyFile : TFileStream;
>   MyWriter : TWriter;
> begin
>   MyFile := TFileStream.Create('MyFile',fmCreate);
>   try
>     MyWriter := TWriter.Create(MyFile,4096);
>     try
>       MyWriter.WriteRootComponent(MyNewComponentOnTheForm);
>     finally
>       MyWriter.Free;
>     end;
>   finally
>     MyFile.Free;
>   end;
> end;

> And code like this to read it:

> var
>   MyFile : TFileStream;
>   MyWriter : TReader;
>   MyNewComponent : TMyComponent;
> begin
>   MyFile := TFileStream.Create('MyFile',fmOpenRead);
>   try
>     MyReader := TReader.Create(MyFile,4096);
>     try
>       MyNewComponent := TMyNewComponent.Create(Self);
>       MyNewComponent :=
>         MyReader.ReadRootComponent(MyNewComponent);
>     finally
>       MyReader.Free;
>     end;
>   finally
>     MyFile.Free;
>   end;
> end;

Other Threads