Thread overview
Load entire file, as a char array.
Sep 03, 2018
Chris Katko
Sep 03, 2018
Neia Neutuladh
Sep 03, 2018
Chris Katko
Sep 03, 2018
bauss
Sep 03, 2018
bauss
Sep 03, 2018
Chris Katko
Sep 03, 2018
Chris Katko
Sep 03, 2018
rikki cattermole
September 03, 2018
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.
September 03, 2018
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.
September 03, 2018
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.

That works great! I thought all file i/o had to through the File class.
September 03, 2018
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.
September 03, 2018
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");
September 03, 2018
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?
September 03, 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!
September 03, 2018
On 03/09/2018 7:38 PM, 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.

Only three. string, wstring and dstring. They are all aliases for immutable(Char)[].

> I can run .strip() on a char[]. But I can't run .replace('\n','?') ?

Replace is working on arrays, use " not '. There is a dedicated version for characters (tr).

> So then I convert char[] to a temporary string and run replace on that.

import std.stdio;
import std.array;

void main()
{
    char[] text = "123\nhi".dup;
    text = text.replace("\n", "?");
    text.writeln;
}

> 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

Looks ok to me:

import std.stdio;

void main()
{
    string s = "text";
    writefln(" - [%s]", s);
}