Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
Delphi 2009 Professional
|
Re: Streamproblem
14. Mär 2004, 18:49
Hallo,
ganz eínfach:
Delphi-Quellcode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TTest = record
a, b, c: integer;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
FStream: TMemoryStream;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
// Stream: TMemoryStream; // globale Instanzen / Variablen sind immer schlecht
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); // OnCreate Event-Handler
begin
FStream:=TMemoryStream.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject); // OnDestroy Event-Handler
begin
FStream.Free
end;
procedure TForm1.Button1Click(Sender: TObject);
var Test: TTest;
begin
Test.a := 1;
Test.b := 2;
Test.c := 3;
{Stream := TMemoryStream.Create;
Stream.WriteBuffer(Test, Sizeof(Test)); }
FStream. Write(Test,SizeOf(TTest);
end;
procedure TForm1.Button2Click(Sender: TObject);
var test2: TTest;
begin
FStream.Position := 0;
FStream.SaveToFile(' c:/hm.txt');
end;
procedure TForm1.Button3Click(Sender: TObject);
// Öffnet eine Datei und schiebt den Inhalt in FStream
var
Test3 : TTest;
begin
FStream.LoadfromFile(' c:/hm.txt');
FStream.Position := 0; // sicherheitshalber
FStream. Read(Test3,SizeOf(TTest));
end;
end.
Ich habe Deinen Code noch ein bißchen geändert.
I come from outer space to save the human race
|
|
Zitat
|