ellapt

T13.1.ReadWriteOddNums

Jan 31st, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. class ReadWriteOddNums
  5. {
  6. static void Main()
  7. {
  8. Console.WriteLine("Read a text file and print its odd lines");
  9. string fileName=@"../../License.txt";
  10. try
  11. {
  12. StreamReader reader = new StreamReader(fileName);
  13. using (reader)
  14. {
  15. int lineNumber = 0;
  16. string line="";
  17. line = reader.ReadLine();
  18. while (line != null)
  19. {
  20. lineNumber++;
  21. if (lineNumber % 2 != 0)
  22. {
  23. Console.WriteLine("Line {0}: {1}", lineNumber, line);
  24. }
  25. line = reader.ReadLine();
  26. }
  27. }
  28. }
  29. catch (Exception ex)
  30. {
  31. Console.WriteLine("Error Reading file {0}: {1}", fileName,ex.Message);
  32. }
  33. finally
  34. {
  35. Console.WriteLine("Only lines with odd line numbers are printed.");
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment