| Nilsener hat folgendes geschrieben: |
Ich vermute, daß der String von Position String[0] bis String[length(String)] gespeichert wird und die Position 0 dabei Probleme macht da sie irgend etwas enthält das mir unbekannt ist.
|
Ich verstehe dein Problem nicht wirklich. Es ist immer ungemein hilfreich wenn du bei Fragen deinen Quellcode postest.
Ich gehe jetzt einfach mal vom worst case aus. Und vermute, dass du die Funktionsweise von writeln falsch werstanden hast, sowie von Short Strings keine Ahnung hast:
Der Pascal Short String ist ein max 256 byte langes Array, der als TP erbsünde in Delphi noch weiterexistiert. Er Zeichnet sich dadurch aus, dass das erste Byte die Länge angibt, also "s[0]" liefert die länge des shortstrings zurück. Wenn man auf den ganzen String zugreifen will gibt man einfach nur "s" an die Länge wird immer automatisch vom Compiler verwaltet und im ersten Byte gesetzt. Unten folgt ein Auszug aus der Delphi hilfe zu diesem Thema.
Nur nebenbei was für viel Verwirrung sorgt, andere Programmiersprachen wie z.B. C verwenden nullterminierte Strings, daher liegt die Vermutung nahe, dass ein Delphi Long String ein nullterminierter String sei. Was ein Trugschluss ist, denn ein Long String ist wegen seiner Abwärtsompatiblität zu TP ein komplexes Typengebilde.
Und hier das leicht modifizierte Beispiel von obbschtkuche:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
| var x: TextFile; s: string[10]; s2: string[20]; begin s:='234567890'; s2:= '12345678912456'; assignfile(x, 'C:\bla.txt'); rewrite(x); writeln(x,s); writeln(x,s2); closefile(x); end; |
| Delphi Hilfe hat folgendes geschrieben: |
A ShortString is 0 to 255 characters long. While the length of a ShortString can change dynamically, its memory is a statically allocated 256 bytes; the first byte stores the length of the string, and the remaining 255 bytes are available for characters. If S is a ShortString variable, Ord(S[0]), like Length(S), returns the length of S; assigning a value to S[0], like calling SetLength, changes the length of S. ShortString uses 8-bit ANSI characters and is maintained for backward compatibility only.
Object Pascal supports short-string types?in effect, subtypes of ShortString?whose maximum length is anywhere from 0 to 255 characters. These are denoted by a bracketed numeral appended to the reserved word string. For example,
var MyString: string[100];
creates a variable called MyString whose maximum length is 100 characters. This is equivalent to the declarations
type CString = string[100];
var MyString: CString;
Variables declared in this way allocate only as much memory as the type requires?that is, the specified maximum length plus one byte. In our example, MyString uses 101 bytes, as compared to 256 bytes for a variable of the predefined ShortString type.
When you assign a value to a short-string variable, the string is truncated if it exceeds the maximum length for the type.
The standard functions High and Low operate on short-string type identifiers and variables. High returns the maximum length of the short-string type, while Low returns zero.
|