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

BoQsc vaidas.boqsc at gmail.com
Wed Dec 6 13:23:06 UTC 2023


It took me a few minutes to get this one right, so that's the 
reason I'm sharing as a thread on the forum.

#### Description
This snippet opens `example.txt` file and reads it line by line.
Using writeln it prints to the standard output stream.
It also have ability to find a matching line. (if statement)

Additions:

`|` character was used to see the lines more clearly in the 
output.
`\r` is important to include in the `if` statement as every line 
includes newline character.






#### Implementation
**./example.txt** - the input file.
```
test
  s

    HelloWorld
t
ff

```

**FileScan.d** - Program.
```
import std;

void main(){

	foreach (line; File("example.txt").byLine){
		writeln("|"~line);
		if (line == "   HelloWorld\r") { writeln("^This Line is 
here."); }
	}

}
```
#### How to run
`rdmd FileScan.d`

#### Result
```
C:\Users\Windows10\Documents\DSearchAndReplace>rdmd FileScan.d
|test
| s
|
|   HelloWorld
^This Line is here.
|t
|ff
```


More information about the Digitalmars-d-learn mailing list