Autor Beitrag
Hanna Bubiskopf
Hält's aus hier
Beiträge: 14

XP
D7
BeitragVerfasst: Fr 04.02.05 14:34 
ich muß dazu sagen, dass ich gerade erst mit Delphi 7 angefangen hab und vorher nur mit VB 6 geproggt habe.
vielleicht kennt jemand ein tutorial für dateifunktionen bzw. dateizugriffe?
oder jemand hat gerade parat was ich brauche: komplettes einlesen einer datei mit allen zeichen in einen string

_________________
Irgendwann muß man ja anfangen anzufangen.
st-matze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 138

Win 3.11, Win 95, Win 98, Win XP
D7 Ent, D6 Pers, (D5 Pers)
BeitragVerfasst: Fr 04.02.05 14:46 
tue dies:
ausblenden Delphi-Quelltext
1:
2:
  sl := TStringlist.create;
  sl.LoadfromFile('MyFile.txt');


in der eigenschaft
ausblenden Delphi-Quelltext
1:
  sl.Text					


hast du dann die komplette datei als string
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Fr 04.02.05 14:46 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
  sInhalt,
  sTemp:   string;
  fDatei:  textfile;
...
begin
  sInhalt := '';
  AssignFile(fDatei, 'c:\blabla.txt');
  Reset(fDatei);
  while not Eof(fDatei) do
  begin
    Readln(fDatei, sTemp);
    sInhalt := sInhalt + sTemp;
  end;
  CloseFile(fDatei);
  showmessage(sInhalt);
end;




MfG


MrSaint :)


EDIT: @st-matze: oder so :)

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 04.02.05 14:56 
ich bevorzuge

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var fm: tfilestream;
s: string;
begin
  fm := tfilestream.create('C:\hallo.txt',fmopenread);
  setlength(s,fm.size);
  fm.read(s[1],fm.size);
  fm.free;
end;


1. weil tstringlist nicht #0 erfasst
2. weil zuviel speicher verbraucht wird wegen sInhalt := sInhalt + sTemp;, dann doch lieber BlockRead machen
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Fr 04.02.05 15:02 
@uall@ogc:
musst du dann die Länge des Strings nicht auf fm.size + 1 setzen? oder steh ich grad auf'm Schlauch?

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 04.02.05 15:06 
ne muss man nicht da s[1] das erste zeichen vom string ist
Hanna Bubiskopf Threadstarter
Hält's aus hier
Beiträge: 14

XP
D7
BeitragVerfasst: Fr 04.02.05 15:48 
Titel: Vielen Dank! Ich habe es in folgende Funktion zusammengef.
:D Vielleicht braucht es ja später nochmal jemand :D
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function LoadFile(FilePath: String): String;
var fm: tfilestream;
s: string;
begin
  fm := tfilestream.create(FilePath, fmopenread);
  setlength(s,fm.size);
  fm.read(s[1],fm.size);  
  fm.free;
  Result:=s;
end;


Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 04.02.05 17:40 
naja würde dann noch "FilePath" durch "FileName" ersetzen und keine lokale varibale benutzen sondern result direkt ansprechen

setlength(result,fm.size);

weiß jetzt nicht ob delphi da unterscheidungen macht, aber dann muss man sich keine gedanken mehr machen
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Fr 04.02.05 17:47 
uall@ogc hat folgendes geschrieben:
naja würde dann noch "FilePath" durch "FileName" ersetzen [...]

Und um ein const erweitern...

_________________
Ciao, Sprint.
Hanna Bubiskopf Threadstarter
Hält's aus hier
Beiträge: 14

XP
D7
BeitragVerfasst: Fr 04.02.05 19:09 
:wink: Und hier meine nun optimierte Funktion nochmals - danke!

:?: PS: Wenn ich das Ganze jetzt doch lieber in einem Bytearray hätte, wie müsste dann meine Funktion lauten?
Bin halt nur neugierig. Will schnell in Delphi reinkommen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
function LoadFile(Const FileName: String): String;
var fm: tfilestream;
begin
  fm := tfilestream.create(FileName, fmopenread);
  setlength(result,fm.size);
  fm.read(result[1],fm.size);
  fm.free;
end;


Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Fr 04.02.05 19:39 
Da gibt es nun sehr viele Möglichkeiten, wie du die Daten in ein Array lesen kannst. Aber weil hier gerade mit TFileStream gearbeitet worden ist, zeige ich dir eine von vielen.
ausblenden Delphi-Quelltext
1:
2:
type
  TMyArray = array of Byte;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure LoadFile(const FileName: Stringvar MyArray: TMyArray);
var
  Handle: THandle;
begin

  Handle := FileOpen(FileName, fmOpenRead);
  if Handle <> INVALID_HANDLE_VALUE then
    with TFileStream.Create(Handle) do
      try
        SetLength(MyArray, Size);
        Read(MyArray[0], Size);
      finally
        Free;
      end;

end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray: TMyArray;
  I: Integer;
begin

  LoadFile('C:\DF.TXT', MyArray);
  for I := 0 to High(MyArray) do
  begin
    // hier mein Code
  end;
  SetLength(MyArray, 0);

end;

_________________
Ciao, Sprint.
MitschL
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 211

Win 98 SE, Win 2000
D5 Pers, D6 Pers und D7 Pro
BeitragVerfasst: Di 08.02.05 13:11 
@Sprint: Ähm; deine LoadFile-Funktion funktioniert bei mir nicht.
TFileStream.Create erlaubt bei mir keine Handles

Hm. Ob das an Delphi 5 liegen kann?

gegrüßt!

_________________
"Bloßes Ignorieren ist noch keine Toleranz." (Theodor Fontane)
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Di 08.02.05 17:09 
MitschL hat folgendes geschrieben:
Ob das an Delphi 5 liegen kann?

Ja. Liegt an Delphi 5.

_________________
Ciao, Sprint.