Autor Beitrag
ShadowThief
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 278



BeitragVerfasst: Mi 27.07.05 15:35 
Hallo,

ich habe ein komisches Problem, habe aber so das Gefühl, dass es dafür eine einfach Lösung gibt.

Und zwar:
Eine Fremdsoftware erstellt Log-Dateien im als .txt-Dateien. Wenn ich diese mit Notepad anschaue, ist alles ganz normal. Bis hierhin wunderbar. Nun wollte ich eine dieser Dateien mit folgendem Code öffnen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
var
  sl: TStringList;

begin

  sl := TStringList.Create();
  sl.LoadFromFile('c:\temp\log.txt');

  Writeln(sl.Text);

  sl.Free;

end;


Problem: in sl.Text steht nicht das drin, was ich auch in Notepad sehe, sondern völliger Datenmüll (Sonderzeichen/Hieroglyphen etc.). Mit Text-Dateien, die ich selbst erstellt habe, funktioniert jedoch der exakt gleiche Code einwandfrei.

Wo liegt hier das Problem?

thx.
shadow.

_________________
"soylent grün ist menschenfleisch!"
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mi 27.07.05 15:44 
Hmm,
es stimmt schon, dass bei Text mehr Zeichen vorhanden sind, als du in Notepad siehst, z.B. Tabulatoren, Zeilenumbrüche, etc. Aber eigentlich müsste WriteLn damit umgehen können. Poste doch mal bitte ein Stücken Logfile!

Gruß,
maxk

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
ShadowThief Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 278



BeitragVerfasst: Mi 27.07.05 15:50 
Ich bin gerade mal auf die Idee gekommen, die Log-Datei in einem Hex-Editor anzuschauen. Die ersten zwei Bytes sind Hex 'FF' und 'FE'. Das ist bei einer normalen Text-Datei, die ich selbst erstellt habe, nicht der Fall.

Was geht hier vor? Hab ich irgendwas verpasst?

P. S.: Ich denke nicht, dass es was bringt, wenn ich die Log-Datei poste, mach ich aber trotzdem mal. Man kann ja nie wissen. Ist im HTML-Fromat:

ausblenden 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:
25:
26:
27:
28:
29:
<HTML>
<BODY BGCOLOR="white"><FONT FACE="Arial, Helvetica">
<CENTER><FONT SIZE="+2" COLOR="#000080"><B>
Client Statistics
</B></FONT></CENTER><BR>
<CENTER>
<FONT COLOR="#505050">
Cell Manager: BACKUPFFM
<BR>
Creation Date: 27.07.2005 16:27:29
</FONT>
</CENTER>
<BR><BR>
<CENTER><TABLE BORDER="1" CELLSPACING="0" CELLPADDING="2" WIDTH="95%">
<TR>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white">Client</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white">Data Written [GB]</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white"># Files</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white"># Objects</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white"># Completed DA</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white"># Failed DA</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white"># Running DA</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white"># Pending DA</FONT></TH>
<TH BGCOLOR="#000080"><FONT FACE="Arial, Helvetica" COLOR="white">Success</FONT></TH>
</TR>
<TR>
.
.
.

_________________
"soylent grün ist menschenfleisch!"
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mi 27.07.05 15:54 
Das bringt in der Tat nichts! Die Sonderzeichen am Anfang sind natürlich merkwürdig. Mir fiele jetzt auch nichts besseres ein, als den Text durchzugehen und zu gucken, ob das Zeichen darstellbar ist und dann einzeln auszugeben.

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
ShadowThief Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 278



BeitragVerfasst: Mi 27.07.05 16:45 
Ich habe irgendwie das Gefühl, dass das eine "andere Art" von Datei ist. Kann aber selber nicht genau sagen, was ich damit meine, hab nur irgendwann mal was in der Richtung aufgeschnappt, keine Ahnung...

_________________
"soylent grün ist menschenfleisch!"
ManuelGS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: Mi 27.07.05 17:18 
Die Log-Datei ist vielleicht in einem anderen Textformat (Unicode etc.) gespeichert. Öffne die Datei mal mit Word oder Wordpad und speicher sie (speichern unter) als Textdokument. Vielleicht bringt das ja etwas...

_________________
"Leben ist gänzlich Bühne und Spiel; so lerne denn spielen
und entsage dem Ernst - oder erdulde das Leid." - Palladas von Alexandria
matze.de
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 576

Win95, Win98 SE, WinXp Prof SP2
D7P, D8P, FPC2.0
BeitragVerfasst: Mi 27.07.05 17:24 
Also bei mir gibt der auch aus was in der TXT steht....
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  sl: TStringList;

begin

  sl := TStringList.Create();
  sl.LoadFromFile('C:\Dokumente und Einstellungen\matze.de\Desktop\123.txt');

  Writeln(sl.Text);

  sl.Free;
  readln;
end.



mfg matze

_________________
si tacuisses, philosophus mansisses.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mi 27.07.05 17:33 
Hallo,

vermute eine Unicode-Datei liegt hier vor.
Unicode-Dateien fangen mit hex FFFE bzw
FEFF (intel/motorola byte order) an.
HTML 4.01-Spezifikation hat folgendes geschrieben:
Um die Chancen einer ordnungsgemäßen Interpretation zu maximieren, wird zudem empfohlen, dass Dokumente, die als UTF-16 übertragen werden, immer mit dem Zeichen »ZERO-WIDTH NON-BREAKING SPACE« beginnen (hexadezimal FEFF, auch »Byte Order Mark« (BOM) genannt), welches bei Byte-Umkehrung zu hexadezimal FFFE wird, einem garantiert unbestimmten Zeichen. So könnte ein Benutzerprogramm, wenn es ein FFFE (hexadezimal) als erstes Bytes des Textes erhält, wissen, dass die übrigen Bytes bis zum Ende des Textes umgekehrt werden müssen.

Quelle:
HTML 4.01-Spezifikation

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 27.07.05 17:47 
Um das herauszufinden, sieh mal in Notepad bei Datei -> Speichern unter... ganz unten nach, welche Codierung eingestellt ist, das sit die der aktuellen Datei...

Wie kann man aber eine Unicode-Datei in Delphi behandeln, damit sie zum Beispiel in anderen Komponenten wie den SynEdit-Komponenten oder so korrekt angezeigt wird?
Das wird wohl, falls dies die Erklärung für das Problem ist, wohl auch die nächste Frage von ShadowThief sein...

Würde mich jedenfalls auch mal interessieren...
ShadowThief Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 278



BeitragVerfasst: Do 28.07.05 08:31 
Ja genau, ihr habt den Nagel auf den Kopf getroffen, es ist eine Unicode-Datei...
Hmm.

Ich habe jetzt diese Procedure gefunden. Damit kann man eine UniCode-Datei laden. Jetzt funktioniert es auch.
Danke nochmal an alle Helfer.

ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Procedure TForm1.LoadUnicodeFile( const filename: String; strings: TStrings );
  Procedure SwapWideChars( p: PWideChar );
    Begin
      While p^ <> #0000 Do Begin
        p^ := WideChar(Swap( Word(p^)));
        Inc( p );
      End; { While }
    End; { SwapWideChars }
  Var
    ms: TMemoryStream;
    wc: WideChar;
    pWc: PWideChar;
  Begin
    ms:= TMemoryStream.Create;
    try
      ms.LoadFromFile( filename );
      ms.Seek( 0, soFromend );
      wc := #0000;
      ms.Write( wc, sizeof(wc));

      pWC := ms.Memory;
      If pWc^ = #$FEFF Then // normal byte order mark
        Inc(pWc)
      Else If pWc^ = #$FFFE Then Begin // byte order is big-endian
        SwapWideChars( pWc );
        Inc( pWc );
      End { If }
      Else ;  // no byte order mark
      strings.Text := WideCharToString( pWc );
    finally
      ms.free;
    end;
  End; { LoadUnicodeFile }

_________________
"soylent grün ist menschenfleisch!"
matze.de
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 576

Win95, Win98 SE, WinXp Prof SP2
D7P, D8P, FPC2.0
BeitragVerfasst: Do 28.07.05 08:44 
Wichtig ist nur, dass du den Code verstehst :)

mfg matze

_________________
si tacuisses, philosophus mansisses.
ShadowThief Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 278



BeitragVerfasst: Do 28.07.05 08:49 
Ja, für dich vielleicht.
Für meinen Chef ist es wichtig, dass das Programm funktioniert :) .

_________________
"soylent grün ist menschenfleisch!"
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 28.07.05 12:57 
Nach dem Else in Zeile 28 sollte es sich eigentlich um gar keine ordentlich erstellte Unicode-Datei handeln, da diese immer eine Signatur wie vorher abgefragt haben sollten.
Deshalb:
Wenn bei einer Datei da falsche Ergebnisse herauskommen, dann sieh nach, ob es überhaupt Unicode ist.
ggf. musst du nach dem Else die Datei ganz normal laden...

Na ja, aber so lange alles funktioniert ist das auch egal, wollte ich nur gesagt haben.

Und der Code ist für mich auch interessant, es gibt da zwar einiges im netz dazu, aber der Code ist sehr kurz und deshalb für mich wohl am besten geeignet (nach ein paar Änderungen, wie zum Beispiel der angesprochenen :wink: )...