Remove lines from a file: Difference between revisions
Content added Content deleted
(+Stata) |
m (Removed (false) comment) |
||
Line 655: | Line 655: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
{{works with|C sharp|6}} |
|||
<lang csharp>using System; |
<lang csharp>using System; |
||
using System.IO; |
using System.IO; |
||
Line 660: | Line 661: | ||
public class Rosetta |
public class Rosetta |
||
{ |
{ |
||
/* C# 6 version: |
|||
public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2); |
public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2); |
||
*/ |
|||
static void RemoveLines(string filename, int start, int count = 1) => |
|||
RemoveLines("foobar.txt", start: 1, count: 2); |
|||
} |
|||
static void RemoveLines(string filename, int start, int count = 1) { |
|||
//Reads and writes one line at a time, so no memory overhead. |
|||
File.WriteAllLines(filename, File.ReadAllLines(filename) |
File.WriteAllLines(filename, File.ReadAllLines(filename) |
||
.Where((line, index) => index < start - 1 || index >= start + count - 1)); |
.Where((line, index) => index < start - 1 || index >= start + count - 1)); |
||
} |
|||
}</lang> |
}</lang> |
||