Converting from std.file.read's void[]

Jonathan M Davis jmdavisProg at gmx.com
Tue Sep 21 18:01:13 PDT 2010


On Tuesday, September 21, 2010 16:41:57 bearophile wrote:
> Jonathan M Davis:
> > UTF-8 strings are easy because they're the same size as ubytes.
> > Casting to char[] for the portion of the data that you want as a string
> > seems to work just fine.
> 
> D2 string are immutable(char)[] and not char[].
> Strings are UTF-8, while the raw bytes you read from a file may contain
> everything, so in some situations you need to use the validate function.

Well, yes. I was talking about strings in the general sense (though UTF-8 
strings), not necessarily the specific type string. The fact that you can cast to 
char[] makes getting strings easy, while the correct way to deal with types 
which aren't bytes isn't as obvious.

> 
> > But what about other types? Is it the correct thing to
> > cast to T[] where T is whatever type the data represents and then index
> > into it to get the values that you want of that type and then cast the
> > next section of the data to U[] where U is the type for the next section
> > of the data, etc.? Or is there a better way to handle this?
> 
> It's better to avoid casts when possible, and SafeD may even be restrict
> their usage. Take a look at the rawWrite/rawRead methods of
> std.stdio.File.

That does look like a better way to handle it. Thanks. Normally, I don't mess 
with binary files, so I'm not particularly well-versed in the correct ways to 
read them.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list