【发布时间】:2016-02-11 20:40:45
【问题描述】:
在 Pascal (Delphi, Lazarus) 中,有 Format() 函数用于从变量列表创建格式化字符串。它的工作方式与C/C++ 中的sprintf() 函数类似。
另一方面,我不知道有任何函数会像sscanf() 在C/C++ 中那样使用格式化字符串设置变量。
我错过了什么?您将如何实现类似的效果?
【问题讨论】:
-
Delphi 的标准库中没有任何等价物。
-
见stackoverflow.com/q/72672/576719。基于其中一些,我制作了一个几乎完整的 sscanf() 函数。
-
我的代码太大,无法发布:(。所以您也可以使用 windows swscanf():
function swscanf(buffer, format: PWideChar): Integer; cdecl; varargs; external 'msvcrt.dll';示例调用:i := swscanf(PChar('42'), PChar('%x'),@j);。 -
感谢您的建议。使用 windows 功能对我没有用,因为我也想(主要)在 linux 下运行程序。 @LU RD 提供的第一个链接似乎很有用,但是 Marco 给出的答案最适合我,因为我使用 Lazarus(基于 FreePascal)。
标签: delphi pascal lazarus formatted-input