Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Truncate-a-file/D/truncate-a-file.d
Normal file
22
Task/Truncate-a-file/D/truncate-a-file.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.file, std.exception;
|
||||
|
||||
void truncateFile(in string name, in size_t newSize) {
|
||||
if (!exists(name) || !isFile(name))
|
||||
throw new Exception("File not found.");
|
||||
|
||||
auto size = getSize(name);
|
||||
if (size <= newSize)
|
||||
throw new Exception(
|
||||
"New size must be smaller than original size.");
|
||||
|
||||
auto content = cast(ubyte[])read(name, newSize);
|
||||
if (content.length != newSize)
|
||||
throw new Exception("Reading file failed.");
|
||||
|
||||
write(name, content);
|
||||
enforce(getSize(name) == newSize);
|
||||
}
|
||||
|
||||
void main() {
|
||||
truncateFile("truncate_test.txt", 0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue