在 Delphi 2010 中将字符串写入 TFileStream

发布于 2024-08-05 08:51:09 字数 774 浏览 5 评论 0原文

我有 Delphi 2007 代码,如下所示:

procedure WriteString(Stream: TFileStream; var SourceBuffer: PChar; s: string);
begin
  StrPCopy(SourceBuffer,s);
  Stream.Write(SourceBuffer[0], StrLen(SourceBuffer));
end;

我这样称呼它:

var
  SourceBuffer : PChar;
  MyFile: TFileStream;

....

SourceBuffer := StrAlloc(1024);
MyFile := TFileStream.Create('MyFile.txt',fmCreate);
WriteString(MyFile,SourceBuffer,'Some Text');
....

这在 Delphi 2007 中有效,但在 Delphi 2010 中给了我很多垃圾字符。我知道这是由于 unicode 合规性问题,但我不确定如何解决这个问题。

这是我到目前为止所尝试过的:

  • 更改的数据类型 SourceBuffer(还有参数 Expected by WideString) 为 PWideChar

  • 列出的每一项建议 这里

我做错了什么?

I have Delphi 2007 code that looks like this:

procedure WriteString(Stream: TFileStream; var SourceBuffer: PChar; s: string);
begin
  StrPCopy(SourceBuffer,s);
  Stream.Write(SourceBuffer[0], StrLen(SourceBuffer));
end;

I call it like this:

var
  SourceBuffer : PChar;
  MyFile: TFileStream;

....

SourceBuffer := StrAlloc(1024);
MyFile := TFileStream.Create('MyFile.txt',fmCreate);
WriteString(MyFile,SourceBuffer,'Some Text');
....

This worked in Delphi 2007, but it gives me a lot of junk characters in Delphi 2010. I know this is due to unicode compliance issues, but I am not sure how to address the issue.

Here is what I've tried so far:

  • Change the data type of
    SourceBuffer(and also the parameter
    expected by WideString) to PWideChar

  • Every one of the suggestions listed
    here

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-08-12 08:51:09

您不需要单独的缓冲区来将字符串写入流。也许最简单的方法是将字符串编码为 UTF8,如下所示:

procedure TStreamEx.writeString(const data: string);
var
   len: cardinal;
   oString: UTF8String;
begin
   oString := UTF8String(data);
   len := length(oString);
   self.WriteBuffer(len, 4);
   if len > 0 then
      self.WriteBuffer(oString[1], len);
end;

function TStreamEx.readString: string;
var
   len: integer;
   iString: UTF8String;
begin
   self.readBuffer(len, 4);
   if len > 0 then
   begin
      setLength(iString, len);
      self.ReadBuffer(iString[1], len);
      result := string(iString);
   end;
end;

我已将 TStreamEx 声明为 TStream 的类助手,但将它们重写为单独的过程和函数(如您的示例)应该不会太困难。

You don't need a separate buffer to write a string to a stream. Probably the simplest way to do it is to encode the string to UTF8, like so:

procedure TStreamEx.writeString(const data: string);
var
   len: cardinal;
   oString: UTF8String;
begin
   oString := UTF8String(data);
   len := length(oString);
   self.WriteBuffer(len, 4);
   if len > 0 then
      self.WriteBuffer(oString[1], len);
end;

function TStreamEx.readString: string;
var
   len: integer;
   iString: UTF8String;
begin
   self.readBuffer(len, 4);
   if len > 0 then
   begin
      setLength(iString, len);
      self.ReadBuffer(iString[1], len);
      result := string(iString);
   end;
end;

I've declared TStreamEx as a class helper for TStream, but it shouldn't be too difficult to rewrite these as a solo procedure and function like your example.

梦忆晨望 2024-08-12 08:51:09

Delphi 2010对此有一个很好的解决方案,记录如下:

http://docwiki.embarcadero .com/CodeExamples/en/StreamStrRdWr_%28Delphi%29

var
  Writer: TStreamWriter;
...

  { Create a new stream writer directly. }
  Writer := TStreamWriter.Create('MyFile.txt', false, TEncoding.UTF8);
  Writer.Write('Some Text');

  { Close and free the writer. }
  Writer.Free();

Delphi 2010 has a nice solution for this, documented here:

http://docwiki.embarcadero.com/CodeExamples/en/StreamStrRdWr_%28Delphi%29

var
  Writer: TStreamWriter;
...

  { Create a new stream writer directly. }
  Writer := TStreamWriter.Create('MyFile.txt', false, TEncoding.UTF8);
  Writer.Write('Some Text');

  { Close and free the writer. }
  Writer.Free();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文