Two little blog/tutorials posts.

Derek Parnell derek at psych.ward
Mon Feb 27 20:27:45 PST 2006


On Tue, 28 Feb 2006 16:15:38 +1300, Regan Heath wrote:

> 
> Oh, and std.file contains a function "read" which will read an entire file  
> into an array. It returns a void[] which can be cast to whatever array  
> type you actually require, eg.
> 
> import std.file;
> import std.stdio;
> 
> void main()
> {
> 	wchar[] text = cast(wchar[])read("utf16.txt");
> 	writefln(text);
> }

Just to clarify, the cast here "cast(wchar[])" does not convert the bytes
read in to UTF16. If you use a cast here, the bytes must already by in the
order implied by the cast. Thus "cast(wchar[])" is useful if the file is
already  UTF16 encoded file. If the file is, say ASCII or UTF8, and you
need it converted to UTF16, ...

    wchar[] text = std.utf.toUTF16(cast(char[])read("utf08.txt"))

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
28/02/2006 3:23:28 PM



More information about the Digitalmars-d-learn mailing list