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