Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Array of String in Datei speichern (https://www.delphipraxis.net/140593-array-string-datei-speichern.html)

sabbert 21. Sep 2009 20:22


Array of String in Datei speichern
 
Nabend ;-),

ich habe zurzeit ein Array of String.

Das Array ist mit vielen Wörtern gefüllt, die ich jetzt gerne in eine Datei speichern möchte.

Beim nächsten Programmstart sollen dann automatisch alle Wörter aus der Datei in das Array geladen werden.

Wie bewerkstellige ich dies?

Delphi-Quellcode:
 var
   myFile : TextFile;
   text  : string;

 begin
   // Try to open the Test.txt file for writing to
   AssignFile(myFile, 'Test.txt');
   Write(myFile);

   // Write a couple of well known words to this file
   WriteLn(myFile, 'Hello');
   //WriteLn(myFile, 'World');

   // Close the file
   CloseFile(myFile);

   // Reopen the file for reading
   Reset(myFile);

   // Display the file contents
   while not Eof(myFile) do
   begin
     ReadLn(myFile, text);
     ShowMessage(text);
   end;

   // Close the file for the last time
   CloseFile(myFile);
 end;
ich hab z.B. jetzt diesen Delphi Code gefunden, aber so ganz verstehe ich nicht wie ich das machen kann das der
das Array füllt?!

z.B.:

Delphi-Quellcode:
for i := 0 to "Dateilänge bzw Menge des Inhalts" - 1 do ArrayString[i] := Inhalt NR. i
also er soll der Rheihe nach vorgehen und alles in das Array speichern?

Wie muss ich da vorgehen?

Klaus01 21. Sep 2009 20:24

Re: Array of String in Datei speichern
 
Guten Abend,

ohne jetzt auf dein aktuelles Problem einzugehen,
aber was bewegt Dich ein Array of String zu nutzen
und nicht gleich eine TStringList (diese bringt sogar
eine Lade- und Speicherroutine mit).

[edit]
zu deinem Problem:
Delphi-Quellcode:

var
   myFile : TextFile;
   text  : string;
   i : Integer:
   sArray : Array of String;

begin
   // Try to open the Test.txt file for writing to
   AssignFile(myFile, 'Test.txt');
   Write(myFile);

   // Write a couple of well known words to this file
   for i:=low(sArray) to high(sArray) do
     WriteLn(myFile, array[i]);

   // Close the file
   CloseFile(myFile);
Einlesen funktioniert analog.
Allerdings solltest Du dann vorher die Größe des Array setzen.
Grüße
Klaus

Luckie 21. Sep 2009 20:40

Re: Array of String in Datei speichern
 
Und jetzt noch eine schöne Fehlerbehandlung drumherum und Luckie ist glücklich. ;)

gammatester 21. Sep 2009 22:57

Re: Array of String in Datei speichern
 
Zitat:

Zitat von Klaus01
Einlesen funktioniert analog.

Leider funktioniert noch nicht mal das rausschreiben:
Delphi-Quellcode:
var
   myFile : TextFile;
   text  : string;
   i : Integer:                 // hier will der Compiler ein ;
   sArray : Array of String;
begin
   AssignFile(myFile, 'Test.txt');
   Write(myFile);               // und hier kracht's immer, richtig rewrite
   for i:=low(sArray) to high(sArray) do
     WriteLn(myFile, array[i]); //und hier wäre sArray angebracht
   CloseFile(myFile);


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:27 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz