delphi XE中如何读取文件内容到字符串
我正在将我的应用程序从 delphi 2007 迁移到 delphi xe,但是读取 ascii 文件并将内容存储在字符串中的过程遇到问题。
这段代码在 Delphi 2007 中有效,
function LoadFileToStr(const FileName: TFileName): String;
var
FileStream : TFileStream;
begin
FileStream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
if FileStream.Size>0 then
begin
SetLength(Result, FileStream.Size);
FileStream.Read(Pointer(Result)^, FileStream.Size);
end;
finally
FileStream.Free;
end;
end;
但是当我在 Delphi XE 中执行这段代码时,结果只是像“?????????”这样的符号。我知道 Delphi XE 是 unicode,所以我将这些行更改
SetLength(Result, FileStream.Size);
FileStream.Read(Pointer(Result)^, FileStream.Size);
为
SetLength(Result, FileStream.Size*2);
FileStream.Read(Pointer(Result)^, FileStream.Size);
将文件的内容存储在 unicode 字符串中,但结果是相同的。
我如何修复此过程以读取此文件的内容?
I'm migrating my application from delphi 2007 to delphi xe, but am having problems with a procedure which reads an ascii file and stores the content in a string.
This code worked in Delphi 2007
function LoadFileToStr(const FileName: TFileName): String;
var
FileStream : TFileStream;
begin
FileStream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
if FileStream.Size>0 then
begin
SetLength(Result, FileStream.Size);
FileStream.Read(Pointer(Result)^, FileStream.Size);
end;
finally
FileStream.Free;
end;
end;
But when I execute this code in Delphi XE the result are just symbols like '????????'. I know that Delphi XE is unicode so I changed these lines
SetLength(Result, FileStream.Size);
FileStream.Read(Pointer(Result)^, FileStream.Size);
to
SetLength(Result, FileStream.Size*2);
FileStream.Read(Pointer(Result)^, FileStream.Size);
to store the content of the file in the unicode string but the result is the same.
how I can fix this procedure to read the content of this file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码不起作用,因为您正在使用 unicode 字符串作为缓冲区读取文件的内容,因此您只是将字节从 TFileStream 的内部缓冲区移动到 unicode 字符串,忽略编码。
您可以轻松修复您的程序,只需将结果类型更改为
AnsiString
但我会建议您使用
TFile.ReadAllText
函数在一行代码中读取文件的内容,同时还处理文件的编码。you code does not work because you are reading the content of the file using a unicode string as buffer, so you are just moving bytes from the internal buffer of the TFileStream to the unicode string ignoring the encoding.
you can fix easily your procedure , just changing the result type to
AnsiString
but i will recommend you which you use the
TFile.ReadAllText
function instead which in a single line of code read the content of a file a also handle the encoding of the file.您可以使用 ReadAllText 函数通过一行代码来实现此目的。像这样:
或者
它将正确检测 ANSI、Unicode 和二进制文件。
You can achieve this with one line of code using ReadAllText function. Like this:
or
It will correctly detect ANSI, Unicode and binary files.
您应该考虑编码,例如:
You should take encoding into account, for example:
我建议使用 TStringList 加载文件的内容,然后将结果设置为variable.Text,例如:
这样您就不必担心任何事情,恕我直言,它将向后和未来兼容。
编辑:如果您需要从 TStream 后代加载,请将 LoadFromFile 替换为 LoadFromStream。
I recommend using a TStringList to load the content of the file and then set the result to variable.Text, for example:
In this way you don't have to worry about anything, it will be backwards and future compatible IMHO.
EDIT: If you need to load from a TStream descendant, then replace LoadFromFile with LoadFromStream.