www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - calling fgets()

reply "Red" <resmith lavabit.com> writes:
How would I call the C library routine fgets() ?

char *fgets(char *s, int size, FILE *stream);

My problem is with the first argument. I am not sure what to 
declare and how to pass it. I tried doing it as you would in C, 
but it doesn't compile.
Dec 23 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Red:

 My problem is with the first argument. I am not sure what to 
 declare and how to pass it. I tried doing it as you would in C, 
 but it doesn't compile.
Take a look at the toStringz function. But often using D functions is better than using C functions. Bye, bearophile
Dec 23 2012
prev sibling parent reply Mike Wey <mike-wey example.com> writes:
On 12/23/2012 02:36 PM, Red wrote:
 How would I call the C library routine fgets() ?

 char *fgets(char *s, int size, FILE *stream);

 My problem is with the first argument. I am not sure what to declare and
 how to pass it. I tried doing it as you would in C, but it doesn't compile.
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)]; -- Mike Wey
Dec 23 2012
parent reply "Red" <resmith lavabit.com> writes:
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).
Dec 24 2012
parent "Regan Heath" <regan netmail.co.nz> writes:
On Mon, 24 Dec 2012 12:00:05 -0000, Red <resmith 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/
Dec 31 2012