Autor Beitrag
cux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Do 07.04.05 19:03 
folgender quellcode soll ein einzelnes wort aus einer datei in einen string einlesen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Button2Click(Sender: TObject);
var
  stream: TFileStream;
  wert: String;  //das wort, welches ausgelesen werden soll
  len: Integer;  //die länge des wortes
begin
  stream:= tfilestream.Create('c:\stream.txt', fmopenread);
  stream.Read(len, sizeof(len));
  stream.Read(PChar(wert)^,len);
  stream.Free;
  label1.Caption:= wert;
end;


das problem ist, dass der string wert anscheinend leer ist. es wird in label1 nichts ausgegeben und die länge des strings ist 0.
ich hoffe mir kann jemand helfen!

cux
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Do 07.04.05 19:11 
Du musst vorher schon den Speicher für den String reservieren:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button2Click(Sender: TObject);
var
  stream: TFileStream;
  wert: String;  //das wort, welches ausgelesen werden soll
  len: Integer;  //die länge des wortes
begin
  stream:= TFileStream.Create('c:\stream.txt', fmOpenRead);
  stream.Read(len, SizeOf(len));
  SetLength(wert, len);
  stream.Read(wert[1], len);
  stream.Free;
  label1.Caption := wert;
end;


Gruß, Motzi

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 07.04.05 19:25 
Außerdem sollte man prüfen, ob überhaupt etwas gelesen werden muss.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.