Thread overview
D Snippet: Reading a file to standard output on Windows Operating System. (FileScan.d)
Dec 06
BoQsc
Dec 06
BoQsc
Dec 08
BoQsc
Dec 09
BoQsc
December 06

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
December 06

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

}
December 08

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

}
December 09

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