www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Load entire file, as a char array.

reply Chris Katko <ckatko gmail.com> writes:
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.
Sep 02 2018
parent reply Neia Neutuladh <neia ikeran.org> writes:
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.
Sep 02 2018
next sibling parent Chris Katko <ckatko gmail.com> writes:
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.
Sep 02 2018
prev sibling parent reply bauss <jj_1337 live.dk> writes:
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.
Sep 02 2018
parent reply bauss <jj_1337 live.dk> writes:
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");
Sep 02 2018
parent reply Chris Katko <ckatko gmail.com> writes:
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?
Sep 03 2018
next sibling parent Chris Katko <ckatko gmail.com> writes:
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!
Sep 03 2018
prev sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
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); }
Sep 03 2018