PDA

View Full Version : sscanf C function equivalent?



Darkhog
12-06-2013, 06:21 PM
Is there equivalent to C standard library function sscanf (http://www.cplusplus.com/reference/cstdio/sscanf/) in Pascal? I'm planning on making simple chatbot and REGEXPs are overkill for things I have in mind, function like that would be enough.

imcold
12-06-2013, 07:40 PM
Yes, there is: http://www.freepascal.org/docs-html/rtl/sysutils/sscanf.html

Darkhog
12-06-2013, 11:34 PM
Hm... pointers are dangerous thing. Is there a version which saves it to array of Variant?

SilverWarior
13-06-2013, 03:12 AM
While this is not an answer to your question but why do you need that. Based on my opinion storing anything but stream of characters (Integer, decimal values, etc.) into strings is bad.
Why bad? Becouse in most ocasions you waste more space. For instance:
storing 2147483647 in numerical type would require you 32 bits (4 bytes) of space. Note that number is actually Maximum 32 bit integer.
storing 2147483647 as series of characters(string) would require 80 bits (10 bytes) of space provided that you save it in AnsiChar string also known as AnsiString. Saving it in Unicode format would require even more space

Darkhog
13-06-2013, 06:09 AM
As I said, I want to make simple chatbot that is capable to make some calculations (e.g. when you ask him "what is sum of 2 and 2?" it should respond 4), etc. Also such function makes parsing files much easier.

imcold
13-06-2013, 06:40 PM
You probably need to use a tokenizer for that kind of things to parse the input correctly, since you don't know what may it consist of (so can't use sscanf). As for file parsing, yes it does make it easier, if you know the structure is fixed. If the structure is variable, well then you're at point 1. Split the input by spaces and decide what type you want to cast the parts to, based on context.