calling fgets()

Regan Heath regan at netmail.co.nz
Mon Dec 31 03:07:52 PST 2012


On Mon, 24 Dec 2012 12:00:05 -0000, Red <resmith at lavabit.com> wrote:

> On Sunday, 23 December 2012 at 16:20:47 UTC, Mike Wey wrote:
>
>>
>> If you declare an char array you could pass it's pointer and length as  
>> the first two arguments.
>>
>> char[] buff = new char[1024];
>> fgets(buff.ptr, buff.length, someStream);
>> buff = buff[0 .. strlen(buff)];
>
> Thanks, that does work (buff.length has to be cast to an int). Which is  
> surprising. I would have thought that a char[] in D would not equate to  
> a char array in C since the D char's are UTF-8,  and that a byte[] would  
> have to be used (byte[] also works with a cast).

Technically you're more or less correct :)

But, if your input is all ASCII then as ASCII is a subset of UTF-8 it  
"just works".  If however your input is not ASCII, but say Chinese  
characters in a different encoding/locale then it will go "bang!" at some  
point, probably when you try to write it back to the screen or foreach  
over it.

Using ubyte[] is the technically correct method IMO.  Then in a perfect  
world you'd call a method to convert that ubyte[] from it's known (has to  
be known or detectable somehow) encoding into UTF-8 for use in your D code.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list