sscanf() equivalent for D using Phobos library?

Bill Baxter wbaxter at gmail.com
Thu Dec 4 16:16:57 PST 2008


On Fri, Dec 5, 2008 at 9:06 AM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> "Spacen Jasset" wrote
>> Jarrett Billingsley wrote:
>>> On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714 <rocknroll714 at gmail.com>
>>> wrote:
>>>> Hi. I discovered D recently and I am very, very impressed. I've
>>>> decided to switch from C/C++ over to D as my programming language of
>>>> choice. Right now I'm porting one of my C++ applications to D, and I
>>>> need some help with a simple conversion.
>>>>
>>>> How would I do this:
>>>>
>>>> if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}
>>>>
>>>> In D? I'm looping through a text file using the File.getline() command
>>>> and splitting each line into two separate variables with the sscanf
>>>> command. The if conditional check is to make sure the line is
>>>> formatted properly (the text file is the settings file for my
>>>> application).
>>>>
>>>> Help please!
>>>>
>>>>
>>>
>>> import std.cstream;, and use din.readf.  readf is documented in
>>> std.stream.  :)
>> This seems to work on a file, but can this be done on a char[] ?
>
> might be talking out of my ass, since I'm a tango user, but I think readf is
> a function used by all streams?  So you just need to make that char[] into a
> stream, and then you get readf functionality, maybe.

I think that's what std.stream.MemoryStream is for, but I might be
talking out of my ass too because I've never used it.  But it looks
like you could say

   scope thestream = new std.stream.MemoryStream(thebuffer);

then use thestream just like it was a file.
You may have to cast(byte[])thebuffer, though.

--bb


More information about the Digitalmars-d-learn mailing list