www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writeln Function while reading a Text File is printing appending text

reply BoQsc <vaidas.boqsc gmail.com> writes:
 C:\Users\vaida\Desktop\Associative Array Sorting> rdmd 
 testingGround.d
 0.  The quick brown fox jumps over the lazy dog
 nonononoahahahaha Sphinx of black quartz, judge my vow.




 congue massa.


 felis.
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? 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, "nononono");
 			
 		} else {
 			writeln( lineNumber, ". ", line);
 		}
 		
 		lineNumber++;
 	}
 }
exampleText.txt
  The quick brown fox jumps over the lazy dog
  Sphinx of black quartz, judge my vow.




 massa.


 felis.
Jun 03 2020
next sibling parent reply ttk <ttk ciar.org> writes:
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.
Jun 03 2020
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
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




 congue massa.


 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++;
 	}
 }
Jun 03 2020
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
 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.
Improved example with the above comments resolved. 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"){
 			if (line[line.length -1] == '\u000D'){
 				line = line.remove(line.length - 1);
 			}
 			writeln(lineNumber, ". hahahahahahaha", line, " nononono");
 			
 		} else {
 			writeln( lineNumber, ". ", line[line.length -1]);
 		}
 		
 		lineNumber++;
 	}
 }
Jun 03 2020
parent reply ttk <ttk ciar.org> writes:
On Wednesday, 3 June 2020 at 19:53:03 UTC, BoQsc wrote:
 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.
Improved example with the above comments resolved.
That works, but consider using chomp() instead. https://dlang.org/phobos/std_string.html#.chomp
Jun 03 2020
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
On Wednesday, 3 June 2020 at 20:05:52 UTC, ttk wrote:
 On Wednesday, 3 June 2020 at 19:53:03 UTC, BoQsc wrote:
 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.
Improved example with the above comments resolved.
That works, but consider using chomp() instead. https://dlang.org/phobos/std_string.html#.chomp
Chomp sounds kind of funny hahaha. Who came up with these function names? Walter Bright? Anyways, Chomp's way looks way more simple. Thanks.
 import std.stdio;
 import std.algorithm;
 import std.string;
 import std.uni : lineSep;
 
 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", chomp(line, "\r"), " 
 nononono");
 			
 		} else {
 			writeln( lineNumber, ". ", line);
 		}
 		
 		lineNumber++;
 	}
 }
Jun 03 2020
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jun 03, 2020 at 08:43:43PM +0000, BoQsc via Digitalmars-d-learn wrote:
 On Wednesday, 3 June 2020 at 20:05:52 UTC, ttk wrote:
[...]
 That works, but consider using chomp() instead.
 
 https://dlang.org/phobos/std_string.html#.chomp
Chomp sounds kind of funny hahaha. Who came up with these function names? Walter Bright? Anyways, Chomp's way looks way more simple. Thanks.
Chomp comes from Perl. T -- For every argument for something, there is always an equal and opposite argument against it. Debates don't give answers, only wounded or inflated egos.
Jun 03 2020
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/3/20 1:43 PM, BoQsc wrote:

 Chomp sounds kind of funny hahaha.
Also consider strip, stripLeft, and stripRight. (Not because they may be funny but because they are useful as well. :) ) Ali
Jun 05 2020
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/3/20 2:23 PM, BoQsc wrote:
 C:\Users\vaida\Desktop\Associative Array Sorting> rdmd testingGround.d
 0.  The quick brown fox jumps over the lazy dog
 nonononoahahahaha Sphinx of black quartz, judge my vow.






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? 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,
"nononono");

         } else {
             writeln( lineNumber, ". ", line);
         }

         lineNumber++;
     }
 }
exampleText.txt
  The quick brown fox jumps over the lazy dog
  Sphinx of black quartz, judge my vow.






\u000D is a carriage return, which means that the terminal moves the insertion point to the front of the line, and writes "nononono" over the original text there. (BTW, you can just do \r instead) How do you fix it? I don't know what your requirements are. I don't know what you are expecting. -Steve
Jun 03 2020