-
Notifications
You must be signed in to change notification settings - Fork 4
/
DeleteOddLines.cs
62 lines (58 loc) · 1.54 KB
/
DeleteOddLines.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Write a program that deletes from given text file all odd lines.
* The result should be in the same file.
*/
using System;
using System.IO;
using System.Collections.Generic;
class DeleteOddLines
{
static void Main(string[] args)
{
try
{
Console.Write("Path to file:");
string path = @Console.ReadLine();
List<string> file = new List<string>();
StreamReader reader = new StreamReader(@path);
using (reader)
{
string line = reader.ReadLine();
int count = 1;
while (line != null)
{
if (count % 2 == 0)
{
file.Add(line);
}
line = reader.ReadLine();
count++;
}
}
StreamWriter writer = new StreamWriter(@path);
using (writer)
{
foreach (var item in file)
{
writer.WriteLine(item);
}
}
}
catch (FormatException fe)
{
Console.Error.WriteLine(fe.Message);
}
catch (FileLoadException fle)
{
Console.Error.WriteLine(fle.Message);
}
catch (FileNotFoundException fnfe)
{
Console.Error.WriteLine(fnfe.Message);
}
catch (AccessViolationException ave)
{
Console.Error.WriteLine(ave.Message);
}
}
}