Want to read a whole file as utf-8

FG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 3 15:55:11 PST 2015


On 2015-02-04 at 00:07, Foo wrote:
> How would I use decoding for that? Isn't there a way to read the file as utf8 or event better, as unicode?

Well, apparently the utf-8-aware foreach loop still works just fine.
This program shows the file size and the number of unicode glyps, or whatever they are called:

     import core.stdc.stdio;
     int main() @nogc
     {
         const int bufSize = 64000;
         char[bufSize] buffer;
         size_t bytesRead, count;
         FILE* f = core.stdc.stdio.fopen("test.d", "r");
         if (!f)
             return 1;
         bytesRead = fread(cast(void*)buffer, 1, bufSize, f);
         if (bytesRead > bufSize - 1) {
             printf("File is too big");
             return 1;
         }
         if (!bytesRead)
             return 2;
         foreach (dchar d; buffer[0..bytesRead])
             count++;
         printf("read %d bytes, %d unicode characters\n", bytesRead, count);
         fclose(f);
         return 0;
     }

Outputs for example this: read 838 bytes, 829 unicode characters

(It would be more complicated if it had to process bigger files.)


More information about the Digitalmars-d-learn mailing list