www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D Snippet: Reading a file to standard output on Windows Operating

reply BoQsc <vaidas.boqsc gmail.com> writes:
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.


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.







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

}
```

`rdmd FileScan.d`


```
C:\Users\Windows10\Documents\DSearchAndReplace>rdmd FileScan.d
|test
| s
|
|   HelloWorld
^This Line is here.
|t
|ff
```
Dec 06 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
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."); }
	}

}
```
Dec 06 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
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."); } } } ```
Dec 08 2023
parent BoQsc <vaidas.boqsc gmail.com> writes:

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


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

```
Dec 09 2023