Autor Beitrag
Biarchiv
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 688



BeitragVerfasst: Mi 09.03.05 18:27 
Hallo,

bei folgenden Code habe ich ein Problem wie ich Char informationen mit einem String per IF vergleichen kann.

Steht im code wo der if Satz ist...

Danke schon mal...

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:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
var filer, stelle : String;
    x : widestring;
    f : file;
    u : array[0..65535of char;
    i, i1, iRead, iWrite : integer;
begin
  {$I-}
  AssignFile(f, Form1.Edit1.Text);
  Reset(f,1);
  filer := '';
  for i := 0 to (Memo1.lines.count -1do
    begin
    stelle := Memo1.lines[i];
    x := Memo1.lines[i];
    for i1 := 0 to (Length(Memo1.lines[i])) do
      begin
      if not (i1 = 0then
        begin
        filer := filer + stelle[i1] + #0;
        end;
      end;
    repeat
      BlockRead(f,u,Length(filer),iRead);
      if (iRead = Length(filer)) then
        begin
// wie kann man hier prüfen
// if(lstrcmp(u,x) = 0) then
// if (u = filer) or (u = x) then  //oder sooo
         begin
         Seek(f, FilePos(F) - Length(filer) - 24);
         showmessage(IntToStr(FilePos(f)));
         fillchar(u,sizeof(u),$ffff01);
         BlockWrite(f,u,3,iWrite);
         Seek(f, FilePos(f) + Length(filer) + 25);
        end;
       end;
     until(iRead = 0);
    end;
  CloseFile(f);
  {$S+}


end;


Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt.
Biarchiv Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 688



BeitragVerfasst: Mo 14.03.05 13:50 
Hallo,

wie lassen sich Char (gelesen aus Dateien) mit Strings per IF vergleichen.

Das würde mir schon reichen, oder eine andere Möglichkeit um was auszulesen damit man es in einem String statt Char bekommt.

Danke
himitsu
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 40



BeitragVerfasst: Mo 14.03.05 16:13 
Wie wäre es mit diesen Varianten?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Var S: String;
  P: PChar;

If (Length(S) = Length(P)) and CompareMem(PChar(S), P, Length(S)) Then StringsSindGleich...

If (Length(S) = Length(P)) and CompareMem(@S, P, Length(S)) Then StringsSindGleich...




If (Length(S) <> Length(P)) or not CompareMem(PChar(S), P, Length(S)) Then SindUnterschiedlich...

If (Length(S) <> Length(P)) or not CompareMem(@S, P, Length(S)) Then SindUnterschiedlich...

_________________
warum einfach wenn's auch kompliziert geht
schreib wie du willst und halt dich an keine standards
Biarchiv Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 688



BeitragVerfasst: Do 17.03.05 17:36 
Hallo,

danke mal für deine Hilfe aber ich hab was falsches gesagt.
Ich meinte Array mit String vergleichen...
u : array[0..65535] of char;
filer : string;

Ich habe es mal so geändert und Delphi zeigt keine Fehler.
Würde es so gehen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
       If (Length(filer) = Length(u)) and CompareMem(PChar(filer), @u, Length(filer)) Then found := 1;
       If (Length(filer) = Length(u)) and CompareMem(@filer, @u, Length(filer)) Then found := 1;
       If (Length(filer) <> Length(u)) or not CompareMem(PChar(filer), @u, Length(filer)) Then found := 0;
       If (Length(filer) <> Length(u)) or not CompareMem(@filer, @u, Length(filer)) Then found := 0;
himitsu
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 40



BeitragVerfasst: Sa 19.03.05 13:38 
Klar geht das ... Was du mit wem vergleichts, ist ja egal.

X mit Y vergleichen ist ja das Selber, wie Y mit X vergleichen ... kann ja locker umgedreht werden ^^


Im endefekt machen diese 4 Abfragen alle das Selbe, du brauchst dir einfach nur die Abfrage aussuchen, welche du brauchst/dir gefällt ;)


PS: CompareMem vergleicht ja zwei "gleichgroße" Speicherbereiche, deßhalb muß Vorher noch die Länge verglichen werden.
Hierbei ist es auch egal, ob CompareMem(CharArray, String, oder CompareMem(String, CharArray, ... CompareMem nimme die Beiden Strings ja eh gleich an (beide werden doch schließlich in einen Pointer umgewandelt, welcher einfach nur auf die entsprechende Speicheradresse zeigt)


Es ginge ja auch noch andersrum > erst CompareMem und dann notfals noxch die Länge,
dieser Vergleich arbeitet halt etwas schneller, da nur verglichen wird, wenn auch beide Strings gleich lang sind - bei unterschiedlicher Länge sind die ja eh unterschiedlich.

ausblenden Delphi-Quelltext
1:
2:
If CompareMem(PChar(S), P, Min(Length(S), Length(P)) and (Length(S) = Length(P))Then 
  StringsSindGleich Else SindUnterschiedlich;

_________________
warum einfach wenn's auch kompliziert geht
schreib wie du willst und halt dich an keine standards