Load entire file, as a char array.
Chris Katko
ckatko at gmail.com
Mon Sep 3 07:45:04 UTC 2018
On Monday, 3 September 2018 at 07:38:51 UTC, Chris Katko wrote:
> On Monday, 3 September 2018 at 06:28:38 UTC, bauss wrote:
>> On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote:
>>> On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh
>>> wrote:
>>>> On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko
>>>> wrote:
>>>>> This should be simple? All I want to do is load an entire
>>>>> file, and access individual bytes. The entire thing. I
>>>>> don't want to have know the file size before hand, or
>>>>> "guess" and have a "maximum size" buffer.
>>>>>
>>>>> So far, all google searches for "dlang binary file read"
>>>>> end up not working for me.
>>>>>
>>>>> Thank you.
>>>>
>>>> http://dpldocs.info/experimental-docs/std.file.read.1.html
>>>>
>>>> import std.file : read;
>>>> auto bytes = read("filename");
>>>>
>>>> This gives you a void[], which you can cast to ubyte[] or
>>>> char[] or whatever you need.
>>>
>>> Or he could do readText() which returns a string, which in
>>> turn will give a proper char array when casted.
>>
>> Actually ignore the casting thing, looking at readText it
>> takes a template parameter.
>>
>> So:
>>
>> char[] a = readText!(char[])("filename");
>
> Thanks, that works!
>
> But... I'm so confused by D's fifty different string types.
>
> I can run .strip() on a char[]. But I can't run
> .replace('\n','?') ?
>
> So then I convert char[] to a temporary string and run replace
> on that.
>
> but then writefln("%s") doesn't accept strings! Only char[].
>
> char []t = cast(char[])(c[i-15 .. i+1]).strip();
> string s = text(t); //s.replace('\n','?')
> writefln(" - [%s]", s); // fail
>
> main.d(89): Error: template std.array.replace cannot deduce
> function from argument types !()(char[], char, char),
> candidates are:
> /usr/include/dmd/phobos/std/array.d(2122):
> std.array.replace(E, R1, R2)(E[] subject, R1 from, R2 to) if
> (isDynamicArray!(E[]) && isForwardRange!R1 && isForwardRange!R2
> && (hasLength!R2 || isSomeString!R2))
> /usr/include/dmd/phobos/std/array.d(2255):
> std.array.replace(T, Range)(T[] subject, size_t from, size_t
> to, Range stuff) if (isInputRange!Range &&
> (is(ElementType!Range : T) || isSomeString!(T[]) &&
> is(ElementType!Range : dchar)))
>
>
> What's going on here?
WAIT! This is my fault (not that I was saying it was "D's" fault,
just that I was confused).
it's not replace '' ''. It's replace "" "". For some reason, I
must have been thinking it was per-character (which is what I'm
doing) so I should be using single quotes.
So I CAN run .replace("","") on a char[], just as I can a string.
And THANK GOODNESS because I thought one of the major advantages
of D was being relatively orthogonal/type agnostic and if I was
going to have to remember "X() runs only on Y" for 3+ different
string types that would be a nightmare!
More information about the Digitalmars-d-learn
mailing list