假设一个字符串a为:123456
edit.text := a;
则显示为:123456假设a为:123#0456
edit.text := a;
则显示为:123文件,pchar也一样……
怎样让这个#0能受控制
比如,写到文件里……
edit.text := a;
则显示为:123456假设a为:123#0456
edit.text := a;
则显示为:123文件,pchar也一样……
怎样让这个#0能受控制
比如,写到文件里……
解决方案 »
- 怎么判断TreeView前面加的复选框是否处于选中状态?
- [正确就结贴]关于VCL中TEdit.Text的字符串是从下标1开始的吗?
- 请问一下,delphi弹出这个错误,应该怎么解决!
- 如何实现同一应用程序的不同实例间参数的传递?
- 用activex控件实现对word的操作
- 请问delphi7如何备份和恢复数据库(ACCESS或SQL SERVER)
- 多表更新问题?
- 最近我接到一个中小企业的信息管理系统的定单临时需要4名程序员协作开发,有意者请回复或mail to me [email protected]
- ===弱智问题?什么是MSHTML 6.0,有什么用?怎么用?===
- 请问一个简单的条件是用Ttable的filter还是Tquery?
- 请赐教一个关于SQL Server中Image字段如何在水晶报表中显示问题
- 大家帮我看看这个统计记录的问题!
array与FileStream吧
Working with null-Terminated Strings
Many programming languages, including C and C++, lack a dedicated string data type. These languages, and environments that are built with them, rely on null-terminated strings. A null-terminated string is a zero-based array of characters that ends with NUL (#0); since the array has no length indicator, the first NUL character s the end of the string. You can use Delphi constructions and special routines in the SysUtils unit (see Standard routines and I/O ) to handle null-terminated strings when you need to share data with systems that use them.
For example, the following type declarations could be used to store null-terminated strings.
type
TIdentifier = array[0..15] of Char;
TFileName = array[0..259] of Char;
TMemoText = array[0..1023] of WideChar;
With extended syntax enabled ({$X+}), you can assign a string constant to a statically allocated zero-based character array. (Dynamic arrays won't work for this purpose.) If you initialize an array constant with a string that is shorter than the declared length of the array, the remaining characters are set to #0.
Using Pointers, Arrays, and String Constants
To manipulate null-terminated strings, it is often necessary to use pointers. (See Pointers and pointer types.) String constants are assignment-compatible with the PChar and PWideChar types, which represent pointers to null-terminated arrays of Char and WideChar values. For example,
var P: PChar;
...
P := 'Hello world!'
points P to an area of memory that contains a null-terminated copy of 'Hello world!' This is equivalent to
const TempString: array[0..12] of Char = 'Hello world!';
var P: PChar;
...
P := @TempString[0];
You can also pass string constants to any function that takes value or const parameters of type PChar or PWideChar - for example StrUpper('Hello world!'). As with assignments to a PChar, the compiler generates a null-terminated copy of the string and gives the function a pointer to that copy. Finally, you can initialize PChar or PWideChar constants with string literals, alone or in a structured type. Examples:
const
Message: PChar = 'Program terminated';
Prompt: PChar = 'Enter values: ';
Digits: array[0..9] of PChar = ('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine');
Zero-based character arrays are compatible with PChar and PWideChar. When you use a character array in place of a pointer value, the compiler converts the array to a pointer constant whose value corresponds to the address of the first element of the array. For example,
var
MyArray: array[0..32] of Char;
MyPointer: PChar;
begin
MyArray := 'Hello';
MyPointer := MyArray;
SomeProcedure(MyArray);
SomeProcedure(MyPointer);
end;
This code calls SomeProcedure twice with the same value.
A character pointer can be indexed as if it were an array. In the previous example, MyPointer[0] returns H. The index specifies an offset added to the pointer before it is dereferenced. (For PWideChar variables, the index is automatically multiplied by two.) Thus, if P is a character pointer, P[0] is equivalent to P^ and specifies the first character in the array, P[1] specifies the second character in the array, and so forth; P[-1] specifies the 'character' immediately to the left of P[0]. The compiler performs no range checking on these indexes.
The StrUpper function illustrates the use of pointer indexing to iterate through a null-terminated string:
function StrUpper(Dest, Source: PChar; MaxLen: Integer): PChar;
var
I: Integer;
begin
I := 0;
while (I < MaxLen) and (Source[I] <> #0) do
begin
Dest[I] := UpCase(Source[I]);
Inc(I);
end;
Dest[I] := #0;
Result := Dest;
end;
Mixing Delphi Strings and Null-Terminated Strings
You can mix long strings (AnsiString values) and null-terminated strings (PChar values) in expressions and assignments, and you can pass PChar values to functions or procedures that take long-string parameters. The assignment S := P, where S is a string variable and P is a PChar expression, copies a null-terminated string into a long string.
In a binary operation, if one operand is a long string and the other a PChar, the PChar operand is converted to a long string.
You can cast a PChar value as a long string. This is useful when you want to perform a string operation on two PChar values. For example,
S := string(P1) + string(P2);
You can also cast a long string as a null-terminated string. The following rules apply.
If S is a long-string expression, PChar(S) casts S as a null-terminated string; it returns a pointer to the first character in S. For example, if Str1 and Str2 are long strings, you could call the Win32 API MessageBox function like this: MessageBox(0, PChar(Str1), PChar(Str2), MB_OK);
You can also use Pointer(S) to cast a long string to an untyped pointer. But if S is empty, the typecast returns nil.
PChar(S) always returns a pointer to a memory block; if S is empty, a pointer to #0 is returned.
When you cast a long-string variable to a pointer, the pointer remains valid until the variable is assigned a new value or goes out of scope. If you cast any other long-string expression to a pointer, the pointer is valid only within the statement where the typecast is performed.
When you cast a long-string expression to a pointer, the pointer should usually be considered read-only. You can safely use the pointer to modify the long string only when all of the following conditions are satisfied.
The expression cast is a long-string variable.
The string is not empty.
The string is unique - that is, has a reference count of one. To guarantee that the string is unique, call the SetLength, SetString, or UniqueString procedure.
The string has not been modified since the typecast was made.
The characters modified are all within the string. Be careful not to use an out-of-range index on the pointer.
buf: array [0..255] of Char;
FS: TFileStream;
I: Integer;
begin
for I := 0 to SizeOf(Buf)-1 do
Buf[I] := #0;
FS := TFileStream.Create('C:\a.bin', fmCreate);
try
FS.Write(Buf, SizeOf(Buf));
finally
FS.Free;
end;
end;
s.Add('aaa'+#0+'bbbb');
s.SaveToFile('c:\1.txt');
也可以存 不过读方法好像不行
读取用 delphi函数 读取
iFileHandle, FileSize:integer;
Buf:string;
begin
iFileHandle := FileOpen('c:\1', fmOpenRead);
if iFileHandle = -1 then
ShowMessage('Open Error')
else begin
FileSize := FileSeek(iFileHandle, 0, 2);
FileSeek(iFileHandle, 0, 0);
SetLength(Buf, FileSize);
FileRead(iFileHandle, Buf[1], FileSize); //[1]必需有
FileClose(iFileHandle);
end;
var
iFileHandle, FileSize:integer;
Buf:string;
begin
Buf :='aaa'#0'bbb';
if (FileExists('c:\a.swm')) then
DeleteFile('c:\a.swm');
try
iFileHandle := FileCreate('c:\a.swm');
if iFileHandle = -1 then
ShowMessage('Create Error')
else begin
FileSeek(iFileHandle, 0, 0);
FileSize := Length(Buf);
FileWrite(iFileHandle, Buf[1], FileSize); //[1]必需有
FileClose(iFileHandle);
end;
finally
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
iFileHandle, FileSize:integer;
Buf:string;
begin
iFileHandle := FileOpen('c:\a.swm', fmOpenRead);
if iFileHandle = -1 then
ShowMessage('Open Error')
else begin
FileSize := FileSeek(iFileHandle, 0, 2);
FileSeek(iFileHandle, 0, 0);
SetLength(Buf, FileSize);
FileRead(iFileHandle, Buf[1], FileSize); //[1]必需有
FileClose(iFileHandle);
end;
end;搞定,结贴