O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required