Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Remove-lines-from-a-file/D/remove-lines-from-a-file.d
Normal file
25
Task/Remove-lines-from-a-file/D/remove-lines-from-a-file.d
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import std.stdio, std.file, std.string;
|
||||
|
||||
void main() {
|
||||
deleteLines("deleteline_test.txt", 1, 2);
|
||||
}
|
||||
|
||||
void deleteLines(string name, int start, int num)
|
||||
in {
|
||||
assert(start > 0, "Line counting must start at 1");
|
||||
} body {
|
||||
start--;
|
||||
|
||||
if (!exists(name) || !isFile(name))
|
||||
throw new FileException("File not found");
|
||||
|
||||
auto lines = readText(name).splitLines();
|
||||
if (lines.length < start + num)
|
||||
throw new Exception("Can't delete lines past the end of file!");
|
||||
|
||||
auto f = File(name, "w");
|
||||
foreach (int i, line; lines) {
|
||||
if (start > i || i >= start + num)
|
||||
f.writeln(line);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue