writeln Function while reading a Text File is printing appending text "before text" and "after text" at the same position
BoQsc
vaidas.boqsc at gmail.com
Wed Jun 3 19:31:04 UTC 2020
On Wednesday, 3 June 2020 at 18:49:38 UTC, ttk wrote:
> On Wednesday, 3 June 2020 at 18:23:51 UTC, BoQsc wrote:
>> Here you can see ". hahahahahahaha" and "nononono" and even
>> lineNumber is being merged into the same position. Why is this
>> happening and can this be simply resolved?
>
> Your string in "line" has a carriage return character at the
> end, which moves the cursor to the beginning of the display row.
>
> If you remove this trailing carriage return character (0x0D) it
> will display correctly.
>
> With the carriage return character in place, everything written
> to your terminal after the carriage return starts at the
> beginning of the row.
It seems to be correct.
Removing the last element of the string got it resolved.
Might not be the best way and adding additional check for
carriage return before removing the element would be better, so
this is only initial proof.
Command Prompt Output
> C:\Users\vaida\Desktop\Associative Array Sorting> rdmd
> associativeArraySorting.d
> 0. The quick brown fox jumps over the lazy dog
> 1. hahahahahahaha Sphinx of black quartz, judge my vow.nononono
> 2. # How vexingly quick daft zebras jump!
> 3. # The five boxing wizards jump quickly
> 4. # Maecenas consectetur risus a lacus sodales iaculis.
> 5. # Morbi sed tortor sollicitudin, pharetra massa egestas,
> congue massa.
> 6. # Sed sit amet nisi at ligula ultrices posuere quis nec est.
> 7. # Mauris vel purus viverra, pellentesque elit id, consequat
> felis.
testingGround.d
> import std.stdio;
> import std.algorithm;
>
>
>
> int lineNumber;
> void main(){
>
> File exampleFile = File("exampleText.txt");
> lineNumber = 0;
> foreach(line; exampleFile.byLine){
>
> if (line == " Sphinx of black quartz, judge my vow.\u000D"){
> writeln(lineNumber, ". hahahahahahaha",
> line.remove(line.length - 1), " nononono");
>
> } else {
> writeln( lineNumber, ". ", line);
> }
>
> lineNumber++;
> }
> }
More information about the Digitalmars-d-learn
mailing list