Load entire file, as a char array.
rikki cattermole
rikki at cattermole.co.nz
Mon Sep 3 07:51:59 UTC 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);
}
More information about the Digitalmars-d-learn
mailing list