Autor Beitrag
dany
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Di 16.12.03 10:59 
Hi,

ich habe einen Stream ( Text ) in ein TMemoryStream Objekt eingelesen ( D1 ). Jetzt möchte ich diesen Stream einer Variablen vom Typ TextFile zuordnen ( D2 ) und dann darauf normal zugreifen, wie ich es von TextFiles gewohnt bin. Hat jemand eine Idee, sowas wie D2 := D1.

dany

_________________
Wenn du den Kopf in den Sand steckst, kuckt dein Hinterteil immer noch raus.
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Di 16.12.03 20:26 
Da musst du wohl einen eigenen TextFile-Treiber schreiben.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
function StreamNOPProc(var t): Integer;
begin
  Result := 0;
end;

function StreamOut(var t: TTextRec): Integer;
begin
  Result := 0;
  if t.BufPos > 0 then
  begin
    TStream(t.Handle).Write(t.BufPtr^, t.BufPos);
    t.BufPos := 0;
  end;
end;

function StreamIn(var t: TTextRec): Integer;
begin
  t.BufEnd := 0;
  t.BufPos := 0;
  with TStream(t.Handle) do
  begin
    t.BufEnd := Size - Position;
    if t.BufEnd > t.BufSize then t.BufEnd := t.BufSize;
  end;
  TStream(t.Handle).Read(t.BufPtr^, t.BufSize);
  Result := 0;
end;

function StreamClose(var t: TTextRec): Integer;
begin
  t.Mode := fmClosed;
  t.InOutFunc := nil;
  t.CloseFunc := nil;
  Result := 0;
end;

function StreamOpen(var t: TTextRec): Integer;
begin
  Result := 0;
  t.BufPos := 0;
  t.BufEnd := 0;
  case t.Mode of
    fmInput: // Reset
      t.InOutFunc := @StreamIn; 

    fmOutput: // Rewrite
      begin
         t.InOutFunc := @StreamOut;
         TStream(t.Handle).Size := 0// Stream leeren
      end;

    fmInOut: // Append
      begin
        t.InOutFunc := @StreamOut;
        TStream(t.Handle).Seek(0, soEnd); // am Ende anfangen
      end;
  end;
  t.CloseFunc := @StreamClose;
end;

procedure AssignStream(var F: Text; Stream: TStream);
begin
  with TTextRec(F) do
  begin
    Mode := fmClosed;
    BufSize := SizeOf(Buffer);
    BufPtr := @Buffer;
    OpenFunc := @StreamOpen;
    InOutFunc := nil;
    FlushFunc := @StreamNOPProc;
    CloseFunc := nil;
    Name[0] := #0;
    Handle := Integer(Stream);
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  F: TextFile;
  MyMemStream: TStringStream;
  S: string;
begin
  MyMemStream := TStringStream.Create('Dies ist eine Text'+sLineBreak+'Du da wie geht es.'+sLineBreak+'In Zeile 3');
  AssignStream(F, MyMemStream);
  Reset(F);
  try
    while not EoF(F) do
    begin
      ReadLn(F, S);
      Memo1.Lines.Add(S);
    end;
  finally
    CloseFile(F);
  end;
end;

_________________
Ist Zeit wirklich Geld?