【发布时间】:2011-09-09 13:10:38
【问题描述】:
我正在使用 Delphi Prism 并使用 BinaryWriter 创建和写入二进制文件,如下所示。
method TUnit.Write(bw:BinaryWriter);
var
i:Integer;
begin
bw.write(ord(uType));
bw.Write(ord(State));
bw.Write(Address);
bw.Write(SubAddress);
for i:=1 to 20 do
bw.Write(fDefs[i]);
end;
我的问题是这个。 write 方法是每行写入一行还是在字节后写入字节或在字符后写入一个字符而不需要换行或回车?
我问这个问题的原因是因为我在编写和读取没有特定数量的字符(如字符数组)的字符串时感到困惑。
例如:
method WritetoFile;
var
x:integer;
thestr:string;
begin
BinaryWriter thefile := new BinaryWriter(File.Create("test.dat"));
thefile.write(thestr);
thefile.write(x);
thefile.Close;
end;
method ReadFromFile;
var
x:integer;
thestr:string;
begin
BinaryReader thefile := new BinaryReader(File.OpenRead("test.dat"));
thestr:=thefile.ReadString;
x:=thefile.ReadInt32;
thefile.Close;
end;
这就是我编写程序的方式,它似乎工作正常,但正如我所说,我很困惑。
当它是字符串数据类型时,它如何知道要读取或写入多少字节或字符长度而不给它特定的读取长度?
【问题讨论】:
标签: .net delphi delphi-prism binaryreader binarywriter