Advertisement
dimipan80

Line Numbers

May 20th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. /* Write a program that reads a text file and inserts line numbers in front of each of its lines. The result should be written to another text file. Use StreamReader in combination with StreamWriter. */
  2.  
  3. namespace _02.Line_Numbers
  4. {
  5.     using System.IO;
  6.     using System.Text;
  7.  
  8.     class LineNumbers
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             using (var reader = new StreamReader("../../LineNumbers.cs"))
  13.             {
  14.                 using (var stream = new FileStream("../../NumberedLines.txt", FileMode.Create))
  15.                 {
  16.                     using (var writer = new StreamWriter(stream, Encoding.ASCII, 4096))
  17.                     {
  18.                         string textLine = reader.ReadLine();
  19.                         int lineNum = 0;
  20.                         while (textLine != null)
  21.                         {
  22.                             lineNum++;
  23.                             writer.WriteLine("#{0,2} {1}", lineNum, textLine);
  24.                             textLine = reader.ReadLine();
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement