Autor Beitrag
obbba
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Mi 11.10.06 14:47 
Ich wollte mal ausprobieren, wie das mit Dateien im Allgemeinen ist.
Im Moment stelle ich mir das so vor, das mit dem Befehl AssignFile() der Inhalt von der Datei auf der Festplatte in einen Bereich (ich glaube das nennt man Buffer) im Arbeitsspeicher geschrieben wird.

Daher hab ich mal folgende Procedur geschrieben:

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:
//[...]
procedure TForm1.Button1Click(Sender: TObject);
var
  datei: file;
  temp: byte;
  i: integer;
  done: boolean;
begin
  i := 0;
  done := false;
  assignfile(datei,Edit1.text);  //ob es die Datei gibt wird nicht geprüft

  while done = false do begin
    temp := byte((datei+i)^);    //die Zeile mag der Debugger nicht
    if temp = EOF then
      done := true
    else
      ListBox1.Items.Add( chr(temp)+'    '+inttostr(temp) );
    i := i+1;
  end;                            //apropos: gibt es sowas wie "break" bei c?
end;
//[...]


Fuktioniert allerdings nicht. Kann mir jemand sagen, wo der Fehler ist?
Vieleicht ist datei gar kein Zeiger, sondern vieleicht nur eine Handle?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 11.10.06 15:07 
Zum einen muss man nach einem Append die Datei erstmal öffnen. Append weist der File-Variablen nur eine Datei zu, nichts weiter. Das geht iirc mit Reset (alternativ rewrite, aber das ist zum lesen nicht so besonders gut geeignet ;-)). Anschließend muss man mit Read aus der Datei lesen. Mehr dazu jeweils in der Hilfe.
Generell würde ich aber empfehlen, mal TFileStreams anzuschauen. Damit ist imho der Zugriff auf Dateien einfacher.

_________________
We are, we were and will not be.
obbba Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Mi 11.10.06 21:20 
Ich hab es jetzt geändert, aber es funktioniert immer noch nicht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure TForm1.Button1Click(Sender: TObject);
var
  datei: file;
  temp: char;
  done: boolean;
begin
  done := false;
  assignfile(datei,Edit1.text);
  reset(datei);

  while done = false do begin
    Read(datei,temp);           //da meckert er
    if EOF(datei) then
      done := true
    else
      ListBox1.Items.Add( temp+'   '+inttostr(byte(temp) ));
  end;

  closefile(datei);
end;


Ich habe mich an der Hilfe, unter dem Titel "Eof, Read, Write (Beispiel)", orientiert.
Da sieht das so aus, als würde read als ersten Parameter ein file verlangen und als zweiten z.B. einen char.
Aber bei mir will er einen untypisierten Pointer.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 11.10.06 21:37 
Moin und :welcome: im Forum!

Probier das mal so: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.Button1Click(Sender: TObject);
  var
    FileStream: TFileStream;
    i: Integer;
    C: Char;
begin
  ListBox1.Clear; // Listbox leeren
  FileStream := TFileStream.Create(Edit1.Text,fmOpenRead or fmShareDenyWrite); // Datei öffnen
  for i := 1 to FileStream.Size do begin // über alle Zeichen der Datei...
    FileStream.Read(C,1); // nächstes Zeichen lesen
    if (C >= #32) then // darstellbar?
      ListBox1.Items.Add(C+' '+IntToStr(Ord(C))) // ja, dann ausgeben
    else
      ListBox1.Items.Add('. '+IntToStr(Ord(C))); // sonst einen Dummy ausgeben
  end;
  FreeAndNil(FileStream); // Datei schließen
end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.


Zuletzt bearbeitet von Narses am Mi 11.10.06 21:39, insgesamt 1-mal bearbeitet
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Mi 11.10.06 21:39 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
var f: file;
    b: byte;

begin
assignfile(f,filename);
reset(f); // oder reset(f,1); ?? oder nur bei blockreads? hmm.. :gruebel:
while not eof(f) do
 begin 
  read(f,b);
  ListBox1.Items.Add(chr(b)); 
   // obwohl ich ner listbox nicht alle bytes zumuten würde wg. steuerzeichen)
 end;
end;


frage:
ListBox1.Items.Add( temp+' '+inttostr(byte(temp) ));

inttostr(byte(temp)) -> what's that? fügst zu das zeichen 2x an?

:r!a:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
obbba Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Mi 11.10.06 21:48 
Naja. Funktioniert. :dunce: .

Warum klappt meins nicht?
Damit werde ich mich Morgen befassen.

Danke für die schnelle Antwort! :zustimm:
obbba Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP
Delphi 5, C mit Dev-C++, PureBasic
BeitragVerfasst: Mi 11.10.06 21:52 
user profile iconrizla hat folgendes geschrieben:
[delphi]

inttostr(byte(temp)) -> what's that? fügst zu das zeichen 2x an?

:r!a:


Ich will das einmal als zeichen und einmal als Zahl.
Ich dachte, dass inttostr ein "int" braucht, und delhalb habe ich das temp gecastet (Ich hatte vermutet, das das so geht).

Anscheinend benutzt man stattdessen ord(temp)
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Mi 11.10.06 22:00 
user profile iconobbba hat folgendes geschrieben:
Anscheinend benutzt man stattdessen ord(temp)
Sollte beides gehen, aber Ord ist kürzer ;)!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.