Want to read a whole file as utf-8
FG via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 3 17:28:33 PST 2015
On 2015-02-04 at 01:56, Namespace wrote:
>
> FILE* f = fopen(filename.ptr, "rb");
> fseek(f, 0, SEEK_END);
> immutable size_t fsize = ftell(f);
> fseek(f, 0, SEEK_SET);
>
That's quite a smart way to get the size of the file.
I started with std.file.getSize (which obviously isn't marked as @nogc) and ended up with the monstrosity below (which I have only compiled on Windows), so I decided not to mention it in my previous post. Wouldn't be the point anyway, since I have only shown an example with a single-fill fixed buffer. But here it is, rendered useless by your code:
long getFileSize(const char* cName) @nogc
{
version(Windows)
{
import core.sys.windows.windows;
WIN32_FILE_ATTRIBUTE_DATA fad;
if (!GetFileAttributesExA(cName, GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, &fad))
return -1;
ULARGE_INTEGER li;
li.LowPart = fad.nFileSizeLow;
li.HighPart = fad.nFileSizeHigh;
return li.QuadPart;
}
else version(Posix)
{
import core.sys.posix.sys.stat;
stat_t statbuf = void;
if (stat(cName, &statbuf))
return -1;
return statbuf.st_size;
}
}
More information about the Digitalmars-d-learn
mailing list