Reading unicode string with readf ("%s")
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 3 12:03:02 PST 2014
On 11/03/2014 11:47 AM, Ivan Kazmenko wrote:
> On Monday, 3 November 2014 at 19:37:20 UTC, Ivan Kazmenko wrote:
>> readf ("%s", &s);
>
> Worth noting: this reads to end-of-file (not end-of-line or whitespace),
> and reading the whole file into a string was what I indeed expected it
> to do.
>
> So, if there is an idiomatic way to read the whole file into a string
> which is Unicode-compatible, it would be great to learn that, too.
I don't know the answer to the Unicode issue with readf but you can read
the file by chunks:
import std.stdio;
import std.array;
import std.exception;
string readAll(File file)
{
char[666] buffer;
char[] contents;
char[] piece;
do {
piece = file.rawRead(buffer);
contents ~= piece;
} while (!piece.empty);
return assumeUnique(contents);
}
void main () {
string s = stdin.readAll();
write (s);
}
Ali
More information about the Digitalmars-d-learn
mailing list