www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.file.read

reply bearophile <bearophileHUGS lycos.com> writes:
Can you tell me why std.file.read() returns a void[] instead of something like
a ubyte[]?

(Performing a cast(ubyte[]) in SafeD can be a problem. I presume in SafeD I
have to use other safer functions to load binary data, like slurp() or
something similar.)

Bye,
bearophile
Aug 01 2010
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 02.08.2010 5:23, bearophile wrote:
 Can you tell me why std.file.read() returns a void[] instead of something like
a ubyte[]?

    

works: ubyte[] data = read("trash.txt"); It's interesting fact deserving further investigation. It seems that void[] arrays are converted implicitly, this also works: void[] tr = malloc(20)[0..20]; data = tr;
 (Performing a cast(ubyte[]) in SafeD can be a problem. I presume in SafeD I
have to use other safer functions to load binary data, like slurp() or
something similar.)

 Bye,
 bearophile
    

Dmitry Olshansky
Aug 02 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Dmitry Olshansky:

 Well, it  magically converts to whatever array type you have.  So this works:
 ubyte[] data = read("trash.txt");

In more than tree years of nearly daily usage of D I have not even tried to write that code :-) Thank you, bearophile
Aug 02 2010
prev sibling parent reply Pelle <pelle.mansson gmail.com> writes:
On 08/02/2010 10:23 AM, Dmitry Olshansky wrote:
 On 02.08.2010 5:23, bearophile wrote:
 Can you tell me why std.file.read() returns a void[] instead of
 something like a ubyte[]?

ubyte[] data = read("trash.txt");

This code does not work for me.
 It's interesting fact deserving further investigation. It seems that
 void[] arrays are converted implicitly, this also works:
 void[] tr = malloc(20)[0..20];
 data = tr;

Neither does this. I am running 2.047, am I doing something wrong?
Aug 02 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Pelle:
 Well, it magically converts to whatever array type you have. So this works:
 ubyte[] data = read("trash.txt");

This code does not work for me on dmd 2.047.

AH, nor for me. Bye, bearophile
Aug 02 2010
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 02.08.2010 21:36, bearophile wrote:
 Pelle:
    
 Well, it magically converts to whatever array type you have. So this works:
 ubyte[] data = read("trash.txt");
        


Bye, bearophile

Ouch, I was very much sure the unittest run, but unfortunately it didn't (it was some other debug output then...). Dang, DMD does not even type check the unitest code when the compiler option not passed. Again, my apologizes. -- Dmitry Olshansky
Aug 02 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Dmitry Olshansky:
 Dang, DMD does not even type check the unitest code when the compiler 
 option not passed.

This is a small known problem of D newsgroups. To partially cope with it at the end of each module I add something like: unittest { printf(__FILE__ ~ " unittest performed."); } But among the enhancement requests of the D unit-testing there need to be a better solution to that problem too. Bye, bearophile
Aug 02 2010