Autor Beitrag
at4germany
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 16:37 
Hallo,
ich möchte mir selber einen sehr einfachen Vokabeltrainer programmieren. Ich hab mir vorgestellt das ich zwei .txt Dateien habe, eine für die Deutschen Vokabeln und eine für die Fremdsprache. Nun wollte ich in das Edit(Deutsch) die erste Zeile der deutsch.txt einlesen. Nur mein Problem ist, wie lese ich denn eine .txt datei bzw. nur die erste Zeile ein, sodass ich diese über "E_Deutsch.text:=" darstellen kann?

Danke schonmal im Vorraus,
lg
Leuchtturm
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1087

Win Vista, Knoppix, Ubuntu
Delphi 7 Pe, Turbo Delphi, C#(VS 2005 Express), (X)HTML + CSS, bald Assembler
BeitragVerfasst: Mo 17.09.07 16:41 
Textdatei in Stringlist einlesen und dann über den index drauf zu greifen
at4germany Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 16:47 
kannst du mir vllt. einen Beispielcode schreiben?
Wäre sehr hilfreich für mich...
lg
at4germany Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 17:20 
Also ich habe bis jetzt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.VokDeutsch;

var s: TStringList;
    z: integer; //Meine Zeile
begin
s.TStringList.Create;
  try
  s.LoadFromFIle('..\Deutsch.txt')


  finally
  s.free
end;


Aber wie lese ich jetzt die einzelnen Zeilen aus? Ich möchte ja später über einen Zufallsgenerator von 1-x die Vokabeln auswählen das diese dann im E_Deutsch.text:= dargestellt werden.

lg

Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Mo 17.09.07 17:25 
ausblenden Delphi-Quelltext
1:
ShowMessage(s.Strings[0]);					


Die Strings sind in einer Liste, die du über den Index ansprechen kannst.

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen
at4germany Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 17:46 
Also ich habe es jetzt mal als function geschrieben, da ich ja noch den zufallsgenerator woanders einbauen wollte. bis jetzt sieht die function so aus:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function TForm1.VokDeutsch: string;
var s: TStringList;
    z: integer;
begin
s.TStringList.Create; //Hier erhalte ich fehler: Undefinierter Bezeichner 'TStringList'
  try
  s.LoadFromFile('C:\..\Deutsch.txt')
  result:= ShowMessage(s.Strings[z]);
  finally
  s.free;
end;


muss ich noch ein constructor hinzufügen oder warum meckert er schon da rum?
Wolle92
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mo 17.09.07 18:01 
Also bei meinem Vokabeltrainer (VokabelFit, in Freeware) spiehcere ich Deutsche und Fremdsprachige Vokabeln in einer Datei... immer jeweils eine neue Zeile und speichere die Abfrage in einem array ask[] und die Antworten in einem array answer[].
Einfach immer ReadLn(f, ask[i]); ReadLn(f, answer[i]);

Zu dienem Problem: s := TStringList.Create;
at4germany Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 18:59 
Super, danke. Jetzt hab ich nurnoch ein Problem.
Wenn ich die Datei compiliere und ausführe, bekomm ich die Meldung, dass die Datei nicht vorhanden ist(a.txt), obwohl sowohl der Pfad, als auch der Name der Datei stimmt. Nur woran liegt das? Ich hab sogar extra zum ausschließen eines Fehlers die datei "nur" a.txt genannt.

Quellcode falls er helfen sollte:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function TForm1.VokDeutsch: string;
var s: TStringList;
    z: integer;
begin
s:= TStringList.Create;
s.LoadFromFile('a.txt');
  try
  result:= s.Strings[0];
  finally
  s.free;
end;
end;


wieso bekomm ich da den Fehler, obwohl der Pfad stimmt?
lg
at4germany Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 19:51 
compilieren geht bis auf den Fehler, dass er sagt das die Dateien nicht gefunden werden...


lg


Zuletzt bearbeitet von at4germany am Mo 17.09.07 22:01, insgesamt 1-mal bearbeitet
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Mo 17.09.07 20:09 
Kompletten Pfad angeben oder mal nach ParamStr schauen.

_________________
Blackheart666
Der Irrsinn ist bei Einzelnen etwas Seltenes, - aber bei Gruppen, Parteien, Völkern, Zeiten die Regel. (Friedrich Nietzsche)
at4germany Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 17.09.07 20:15 
Okay, fehler ist elemeniert... hatte das txt doppelt 8)
aber hab jetzt noch ein Problem. Muss ich irgendwie ein Ende Setzen bei den Dateien? Weil ich hab jetzt zwei Zeilen geschrieben und wenn ich starte sagt er mir:
"Im Projekt Project1.exe ist eine Exception der Klasse EStringListError aufgetreten. Meldung 'Listenindex überschreitet das Maximum (9967532)'. Prozess wurde angehalten. Mit Einzelne Anweisung oder Start fortsetzen."

lg

Edit: Probelm ist auch gelöst... Z war sowohl als Globale, als auch als lokale var. deklariert...