digitalmars.D.learn - Load entire file, as a char array.
- Chris Katko (7/7) Sep 02 2018 This should be simple? All I want to do is load an entire file,
- Neia Neutuladh (6/13) Sep 02 2018 http://dpldocs.info/experimental-docs/std.file.read.1.html
- Chris Katko (3/18) Sep 02 2018 That works great! I thought all file i/o had to through the File
- bauss (3/18) Sep 02 2018 Or he could do readText() which returns a string, which in turn
- bauss (5/27) Sep 02 2018 Actually ignore the casting thing, looking at readText it takes a
- Chris Katko (23/52) Sep 03 2018 Thanks, that works!
- Chris Katko (11/67) Sep 03 2018 WAIT! This is my fault (not that I was saying it was "D's" fault,
- rikki cattermole (20/62) Sep 03 2018 Only three. string, wstring and dstring. They are all aliases for
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
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
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:That works great! I thought all file i/o had to through the File class.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
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:Or he could do readText() which returns a string, which in turn will give a proper char array when casted.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
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:Actually ignore the casting thing, looking at readText it takes a template parameter. So: char[] a = readText!(char[])("filename");On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:Or he could do readText() which returns a string, which in turn will give a proper char array when casted.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
On Monday, 3 September 2018 at 06:28:38 UTC, bauss wrote:On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote: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?On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh wrote:Actually ignore the casting thing, looking at readText it takes a template parameter. So: char[] a = readText!(char[])("filename");On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:Or he could do readText() which returns a string, which in turn will give a proper char array when casted.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 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: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!On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote: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?On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh wrote:Actually ignore the casting thing, looking at readText it takes a template parameter. So: char[] a = readText!(char[])("filename");On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:Or he could do readText() which returns a string, which in turn will give a proper char array when casted.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 03 2018
On 03/09/2018 7:38 PM, Chris Katko wrote:On Monday, 3 September 2018 at 06:28:38 UTC, bauss wrote:Only three. string, wstring and dstring. They are all aliases for immutable(Char)[].On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote:Thanks, that works! But... I'm so confused by D's fifty different string types.On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh wrote:Actually ignore the casting thing, looking at readText it takes a template parameter. So: char[] a = readText!(char[])("filename");On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:Or he could do readText() which returns a string, which in turn will give a proper char array when casted.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.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); // failLooks ok to me: import std.stdio; void main() { string s = "text"; writefln(" - [%s]", s); }
Sep 03 2018