D Snippet: Reading a file to standard output on Windows Operating System. (FileScan.d)

BoQsc vaidas.boqsc at gmail.com
Fri Dec 8 17:53:41 UTC 2023


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."); }
	}

}
```



More information about the Digitalmars-d-learn mailing list