4

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?

4
  • 1
    There's nothing equivalent in the standard library for Delphi. Commented Feb 11, 2016 at 20:46
  • See stackoverflow.com/q/72672/576719. Based on some of that I made an almost complete sscanf() function. Commented Feb 11, 2016 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);. Commented Feb 11, 2016 at 21:22
  • 1
    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). Commented Feb 12, 2016 at 10:39

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.