1.6. Formatting Output

In addition to writing each line of text to the output file, let's extend the previous code segment to also write the line to the user's console. When writing to the console, however, we want to indicate the line number and the length of the line in characters, as well as the text itself. In addition, we don't want to echo an empty line. Here is the relevant portion of the modified code segment:

 string text_line; int line_cnt = 1; while (( text_line = freader.ReadLine() ) != null ) { // don't format empty lines if ( text_line.Length == 0 ) { Console.WriteLine(); continue; } // format output to console: // 1 (42): Master Timothy Gnome left home one morning Console.WriteLine( "{0} ({2}): {1}", line_cnt++, text_line, text_line.Length ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.