3

In Pascal (Delphi, Lazarus), there is the Format() function for creating a formatted string from a list of variables. It works in a similar way to sprintf() function in C/C++.

On the other hand, I am not aware of any function which would set variables using a formatted string as sscanf() does in C/C++. Did I miss something? How would you achieve a similar effect?

Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
Plzak
  • 43
  • 3
  • There's nothing equivalent in the standard library for Delphi. – David Heffernan Feb 11 '16 at 20:46
  • See http://stackoverflow.com/q/72672/576719. Based on some of that I made an almost complete sscanf() function. – LU RD Feb 11 '16 at 20:54
  • My code was too large to post :(. So you can use the windows swscanf() as well: `function swscanf(buffer, format: PWideChar): Integer; cdecl; varargs; external 'msvcrt.dll';` Example call: `i := swscanf(PChar('42'), PChar('%x'),@j);`. – LU RD Feb 11 '16 at 21:22
  • Thanks for suggestions. The use of windows functions is not useful for me, as I want to run the program also (and mainly) under linux. The first link provided by @LU RD seems to be useful, however the answer given by **Marco** suits me best, as I use Lazarus (based on FreePascal). – Plzak Feb 12 '16 at 10:39

1 Answers1

4

Free Pascal has a simple sscanf and a special scandatetime to reverse date format strings (formatdatetime opposite)

From what I see these routines should work with Delphi too with at worst token modifications.

Note that there is also writestr and readstr which are more pascal like formatted I/O concepts similar to write/readln but then to/from strings. These are compiler builtin, so won't work with Delphi

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89