Re: Any additions for write-to-file short program
On Thursday, 18 November 2021 at 22:20:48 UTC, pascal111 wrote: In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical additions. If "the task" is to copy a file, then this program, as presented, may fail at it. If you're going to do any exception handling at all, a question must be answered: what to do in case the algorithm is interrupted before completing? I.e. if the `while` loop throws an exception, which it can do on any line except for braces. Is it acceptable to leave the output file containing only part of input data, perhaps even no data at all? Or should you catch the exception and delete the output file? Will that even be possible?..
Re: Any additions for write-to-file short program
On Thu, Nov 18, 2021 at 10:20:48PM +, pascal111 via Digitalmars-d-learn wrote: > In next program that rewrites original written texts into new files, I > see that it may need some additions or we can accept it like this > because it's just a simple program that achieve its task and doesn't > need any philosophical additions. I assume there's a reason you're reading the input file by line instead of just copying it using larger fixed-size blocks? Perhaps you have in mind some kind of line-based filtering or processing eventually? Because for copying a file, using a large, fixed-size block will work much faster. Not to mention the code will be simpler. For example: auto inputFile = File(inputFilename, "r"); auto outputFile = File(outputFilename, "w"); enum bufferSize = 8194; inputFile.byChunk(bufferSize) // read input in blocks of 8194 bytes .copy(outputFile.lockingBinaryWriter); // copy each block into output file T -- Javascript is what you use to allow third party programs you don't know anything about and doing you know not what to run on your computer. -- Charles Hixson
Any additions for write-to-file short program
In next program that rewrites original written texts into new files, I see that it may need some additions or we can accept it like this because it's just a simple program that achieve its task and doesn't need any philosophical additions. Example: Original text: Learning C doesn't impact bad on C++. In theoretical speaking we think that C++ is C with some new features, but these features maybe tends to some high level, and working with C is more difficult than direct learning of C++, because last one provides features or tools make us pass some low level ways we have to use in C to do same tasks, so, the work with C impact good on learning C++ because we see everything with its real nature in C. New produced text in new file: Learning C doesn't impact bad on C++. In theoretical speaking we think that C++ is C with some new features, but these features maybe tends to some high level, and working with C is more difficult than direct learning of C++, because last one provides features or tools make us pass some low level ways we have to use in C to do same tasks, so, the work with C impact good on learning C++ because we see everything with its real nature in C. Code: // D programming language import std.stdio; import std.string; int main() { string s; char[] f; try{ write("Enter file name and path: "); readln(f); f=strip(f);} catch(Exception err){ stderr.writefln!"Warning! %s"(err.msg);} File file = File(f, "r"); File file2 = File("output_x.txt", "w"); while (!file.eof()) { s = chomp(file.readln()); file2.writeln(s); } file.close(); file2.close(); return 0; }
Re: writeln the struct from the alis this Example from the home page
On 11/18/21 2:58 PM, Jordan Wilson wrote: On Thursday, 18 November 2021 at 16:08:22 UTC, Paul Backus wrote: On Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote: [...] You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method. I thought private was to the module/file, not class/struct? `writeln` is not in your module. -Steve
Re: writeln the struct from the alis this Example from the home page
On Thursday, 18 November 2021 at 16:08:22 UTC, Paul Backus wrote: On Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote: [...] You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method. I thought private was to the module/file, not class/struct? Jordan
Re: writeln the struct from the alis this Example from the home page
On Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote: Hello, if you take the example from the home page, with the additional last line: ```d struct Point { private double[2] p; // Forward all undefined symbols to p alias p this; double dot(Point rhs) { return p[0] * rhs.p[0] + p[1] * rhs.p[1]; } } void main() { import std.stdio : writeln; // Point behaves like a `double[2]` ... Point p1, p2; p1 = [2, 1], p2 = [1, 1]; assert(p1[$ - 1] == 1); // ... but with extended functionality writeln("p1 dot p2 = ", p1.dot(p2)); // additional line: writeln(p1); // is not possible ! } ``` /usr/include/dmd/phobos/std/format.d(3193): Error: no [] operator overload for type Point .. ... How to define, that for Point the same formatting should be used as for double[2] ? You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method.
writeln the struct from the alis this Example from the home page
Hello, if you take the example from the home page, with the additional last line: ```d struct Point { private double[2] p; // Forward all undefined symbols to p alias p this; double dot(Point rhs) { return p[0] * rhs.p[0] + p[1] * rhs.p[1]; } } void main() { import std.stdio : writeln; // Point behaves like a `double[2]` ... Point p1, p2; p1 = [2, 1], p2 = [1, 1]; assert(p1[$ - 1] == 1); // ... but with extended functionality writeln("p1 dot p2 = ", p1.dot(p2)); // additional line: writeln(p1); // is not possible ! } ``` /usr/include/dmd/phobos/std/format.d(3193): Error: no [] operator overload for type Point .. ... How to define, that for Point the same formatting should be used as for double[2] ?