Re: D Snippet: Reading a file to standard output on Windows Operating System. (FileScan.d)
`switch` statement to match a text line While outputing to the standard stream, it is also possible to use `switch` statement to **match a text line to a text line in a file.** I personally think it improve readability and maintainability in some applications. ``` import std; void main(){ foreach (line; File("example.txt").byLine(No.keepTerminator, "\r\n")){ switch(line) { case " HelloWorld" : writeln("|"~line~"|"); writeln("^This Line is here."); break; default : writeln("|"~line~"|"); } } } ``` Reading file, matching line, changing the line, outputting to both: to a standard stream and another file; line by line In this snippet, we are not only reading a file, editing its output and outputting to the standard output stream, but also try to save changes on another file: filling it with the edited output. ``` import std; void main(){ File edit = File("example2.txt", "w"); foreach (line; File("example.txt").byLine(No.keepTerminator, "\r\n")){ switch(line) { case " HelloWorld": edit.write("RandomWord\n"); writeln("|"~"RandomWord"~"|"); break; default : edit.write(line ~ "\n"); writeln("|" ~ line ~ "|"); } } edit.close; } ``` **Inputs:** **Example.txt** ``` test s HelloWorld t ff ``` **Outputs:** **stdout:** ``` |test| | s| | | |RandomWord| |t| |ff| ``` **Example2.txt** ``` test s RandomWord t ff ```
Re: D Snippet: Reading a file to standard output on Windows Operating System. (FileScan.d)
On Wednesday, 6 December 2023 at 14:56:50 UTC, BoQsc wrote: As of recent observation the `line` in the previous implementation seem to recognise **\r** as default terminator. Making `writeln("|" ~ line ~ "|");` not possible. By correcting terminator and disabling it on `byLine` function it is possible to achieve following output to standard stream: **Output** ``` |test| | s| | | | HelloWorld| ^This Line is here. |t| |ff| ``` **Input (example.txt)** ``` test s HelloWorld t ff ``` **Source code** ``` import std; void main(){ foreach (line; File("example.txt").byLine(No.keepTerminator, "\r\n")){ writeln("|"~line~"|"); if (line == " HelloWorld") { writeln("^This Line is here."); } } } ``` [`strip();`](https://dlang.org/library/std/string/strip.html) can be used to achieve same result as `No.keepTerminator, "\r\n"` of `.byLine` ``` import std; void main(){ foreach (line; File("example.txt").byLine()){ writeln("|" ~ strip(line) ~ "|"); if (line == " HelloWorld") { writeln("^This Line is here."); } } } ```
Re: D Snippet: Reading a file to standard output on Windows Operating System. (FileScan.d)
As of recent observation the `line` in the previous implementation seem to recognise **\r** as default terminator. Making `writeln("|" ~ line ~ "|");` not possible. By correcting terminator and disabling it on `byLine` function it is possible to achieve following output to standard stream: **Output** ``` |test| | s| | | | HelloWorld| ^This Line is here. |t| |ff| ``` **Input (example.txt)** ``` test s HelloWorld t ff ``` **Source code** ``` import std; void main(){ foreach (line; File("example.txt").byLine(No.keepTerminator, "\r\n")){ writeln("|"~line~"|"); if (line == " HelloWorld") { writeln("^This Line is here."); } } } ```