Autor Beitrag
GericasS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 22.10.07 07:49 
Guten Morgen DF,

ich hab heute ein Programm geschrieben, das Random Zahlen im Bereich 0-1.000.000 ermittelt.
Diese Zahlen werden in einem Label dargestellt. Wie kann ich diese Zahlen in einem String speichern und anschließend in ein .txt file speichern ?


ausblenden 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:
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := true ;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  randomize ;
    Label1.Caption := IntToStr(random(1000000)) ;
      if
        Label1.Caption = edit1.text then
          Form1.Close ;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled := false ;
end;

end.



MFG

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
uko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 220
Erhaltene Danke: 1

Win XP, VISTA, WIndows 7
Delphi 2007/2010 Prof
BeitragVerfasst: Mo 22.10.07 08:02 
Morgen,

mal ein paar Stichworte:
Textfile, WriteLn, private field

Grüße,
Uli
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mo 22.10.07 08:04 
oder wenn es mehrere Zahlen auf einmal sein sollen und du die schön untereinander anordnen willst wie ne liste dann mit einer Stringlist.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mo 22.10.07 09:11 
Hallo,

ich würde nur randseed speichern und dieses auslesen ;-)

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Falls Datei vorhanden 
  TextDatei öffnen;
  readln(TextDatei,randseed);
  readln(TextDatei,OberesLimit);
  readln(TextDatei,AnzahlZuf)
sonst
  TextDatei erstellen und öffnen;
  randomize;
  writeln(TextDatei,randseed);
  writeln(TextDatei,OberesLimit)
  readln(TextDatei,AnzahlZuf)
Textdatei schliessen

und jetzt die Zahlen neu erzeugen ohne einzulesen:
For i := 1 to AnzahlZuf do
  begin
  ZufZahl := random(OberesLimit),
  MachWasMit(ZufZahl)


Gruß Horst
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 22.10.07 09:45 
user profile iconelundril hat folgendes geschrieben:
oder wenn es mehrere Zahlen auf einmal sein sollen und du die schön untereinander anordnen willst wie ne liste dann mit einer Stringlist.

lg elundril


zur TStringList, habe ich folgendes in der DHilfe gefunden

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
StringList := TStringList.Create;
  try
    with StringList do begin
      Add('In diesem Beispiel wird eine Stringliste verwendet.');
      Add('Dies ist die einfachste Möglichkeit zum Anfügen von Strings');
      Add('an die Stringliste eines Kombinationsfelds.');
      Add('Zur Erinnerung: die TStrings.Create-');
      Add('Methode ist abstrakt; verwenden Sie stattdessen');
      Add('die Methode TStringList.Create.');
    end;

    with ComboBox1 do begin
      Width := 210;
      Items.Assign(StringList);
      ItemIndex := 0;
    end;
  finally
    StringList.free;
  end;


Aber was ich nicht verstehe, wie soll ich das auf die random zahlen beziehen !?

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 22.10.07 09:52 
Moin!

user profile iconGericasS hat folgendes geschrieben:
Aber was ich nicht verstehe, wie soll ich das auf die random zahlen beziehen !?

Grundlagen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
StringList := TStringList.Create; // Objekt erzeugen
try // Resourcen-Schutzblock
  // Dinge tun...
finally // auf jeden Fall
  StringList.Free; // das Objekt wieder freigeben
end;

Erweiterungen:
ausblenden Delphi-Quelltext
1:
2:
3:
StringList.Add(IntToStr(4)); // Eine Zahl als String einfügen

StringList.SaveToFile('C:\Test.txt'); // Inhalt in eine Datei speichern

Die Lösung des Problems ergibt sich durch eine passende Anordnung und Erweiterung der vorgestellten Ansätze. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
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: Mo 22.10.07 09:56 
Zusätzlich sollte auch irgendwo noch LoadFromFile mit auftauchen ...

_________________
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.
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 22.10.07 10:04 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.FormCreate(Sender: TObject);
begin
StringList := TStringList.Create;
  try
    with StringList do
    begin
      StringList.Add(StrToInt(Label1.Caption)) ;
        finally
          StringList.Free ;
            StringList.SaveToFile('C:\Test.txt');

end;

end.


[Fehler] Unit1.pas(62): E2010 Inkompatible Typen: 'string' und 'Integer'

da komme ich jetzt leider nicht mehr weiter ! :oops:
Kann es daran liegen, das ich in der highlight Zeile, den Strtoint befehl gegeben habe und das er die Integer werte nicht speichern kann in der StringList ?!

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 22.10.07 10:13 
Moin!

Trotz der Temperaturen draussen, bitte langsam Gehirn aktivieren und mitdenken! ;)

IntToStr()ist selbstverständlich nur für Integer-Variablen sinnvoll, TLabel.Cpationist schon ein String. :idea:

Wenn du nicht weisst, was withtut, lass es weg! :roll:

Du gibst die Stringliste frei und willst dann den Inhalt speichern?! :shock:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 22.10.07 10:31 
Hey,

o.O tut mir leid wegen dem unnötigen Post ! :flehan: *umverzeiungfleh*

Ich hab jetzt nochmal so einiges geändert. Die aktuelle Zahl des Labels speichert er jetzt in der .txt aber immernoch nicht die davor !?

ausblenden 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:
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := true ;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  randomize ;
        Label1.Caption := IntToStr(random(1000000)) ;
        StringList := TStringList.Create;
    with StringList do
      begin
        StringList.Add(Label1.Caption) ;
        StringList.SaveToFile('C:\Test.txt');
      end;
  end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled := false ;
end;

end.

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 22.10.07 10:39 
user profile iconGericasS hat folgendes geschrieben:
o.O tut mir leid wegen dem unnötigen Post ! :flehan: *umverzeiungfleh*
Das ist noch unnötiger... :roll: Einfach nur Mitdenken würde schon reichen. :D

user profile iconGericasS hat folgendes geschrieben:
Ich hab jetzt nochmal so einiges geändert. Die aktuelle Zahl des Labels speichert er jetzt in der .txt aber immernoch nicht die davor !?
user profile iconBenBEs Hinweis gelesen? ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 22.10.07 10:41 
hey,

war ja auch nur Spaßhalber gemeint :)
werde Bens Vorschalg gleich mal umsetzten !

Danke für eure Hilfe bis hierhin !

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300