字符串到 TStream

发布于 2024-07-25 23:11:36 字数 423 浏览 2 评论 0原文

我正在尝试将字符串转换为 TStream。 下面的代码在 CopyFrom 行上给出了“抽象错误”消息。 我在这里遇到了一堵砖墙,关于如何解决这个问题有什么想法吗?

procedure StringToStream(const AString: string; out AStream: TStream);
var
  SS: TStringStream;
begin
  SS := TStringStream.Create(AString);
  try
    SS.Position := 0;
    AStream.CopyFrom(SS, SS.Size);  //This is where the "Abstract Error" gets thrown
  finally
    SS.Free;
  end;
end;

I'm attempting to convert a string to a TStream. My code below gives me an "Abstract Error" message on the CopyFrom line. I'm against a brick wall here, any ideas on how to solve this?

procedure StringToStream(const AString: string; out AStream: TStream);
var
  SS: TStringStream;
begin
  SS := TStringStream.Create(AString);
  try
    SS.Position := 0;
    AStream.CopyFrom(SS, SS.Size);  //This is where the "Abstract Error" gets thrown
  finally
    SS.Free;
  end;
end;

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

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

发布评论

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

评论(4

贱人配狗天长地久 2024-08-01 23:11:36

以下过程应该完全符合您的要求。 请注意,您对 AStream 的使用负责释放在此过程中创建的实例。 返回父类(在本例中为 tStream)而不是特定的后代是完全可以的。

procedure StringToStream(const AString: string; out AStream: TStream);
begin
  AStream := TStringStream.Create(AString);
end;

您还可以将其编码为函数:

Function StringToStream(const AString: string): TStream;
begin
  Result := TStringStream.Create(AString);
end;

The following procedure should do excactly what your looking for. Please note that your usage of AStream is responsible for freeing the instance that is created in this procedure. It is perfectly fine to return the parent class (in this case tStream) rather than the specific descendant.

procedure StringToStream(const AString: string; out AStream: TStream);
begin
  AStream := TStringStream.Create(AString);
end;

You can also code this as a function:

Function StringToStream(const AString: string): TStream;
begin
  Result := TStringStream.Create(AString);
end;
浅暮の光 2024-08-01 23:11:36

AStream 被声明为 OUT 参数,这意味着它没有在过程开始时分配,过程负责为其分配适当的值。

如果我正确解释您的代码,您应该省略 OUT 并确保在调用例程时正确实例化 AStream。

更多显示 StringToStream 调用的代码可能会提供更多线索。

AStream is declared as OUT parameter, which means it isn't assigned at the beginning of the procedure and the procedure is responsible to assign a proper value to it.

If I interpret your code correct, you should omit the OUT and make sure AStream is instantiated properly when you call the routine.

Some more code showing the call of StringToStream may give some more clues.

风向决定发型 2024-08-01 23:11:36

CopyFrom 调用 ReadBuffer,ReadBuffer 调用 Read,并且 Read 被声明为抽象。 您要传递给 AStream 的流类型是什么? 如果它没有实现 Read,你会在那里得到一个抽象错误。 (当你实例化它时,编译器应该给你一个警告。)

CopyFrom calls ReadBuffer, which calls Read, and Read is declared abstract. What sort of stream are you passing to AStream? If it doesn't implement Read, you'll get an abstract error there. (And the compiler should give you a warning when you instantiate it.)

冷弦 2024-08-01 23:11:36

在我看来,将 AStream 声明为 out 是错误的。 尝试删除掉。

如果这没有帮助,这是我使用的函数:

procedure StringToStream(Stream: TStream;const S: String);
begin
Stream.Write(Pointer(S)^, length(S));
end;

Declaring AStream as out looks wrong to me. Try removing the out.

If that doesn't help, here is the function I use:

procedure StringToStream(Stream: TStream;const S: String);
begin
Stream.Write(Pointer(S)^, length(S));
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文