Truncate a file: Difference between revisions

Content added Content deleted
(Added BBC BASIC)
m (→‎{{header|D}}: check isFile)
Line 249: Line 249:


void truncateFile(string name, size_t newSize) {
void truncateFile(string name, size_t newSize) {
if (!exists(name))
if (!exists(name) || !isFile(name))
throw new Exception("File not found");
throw new Exception("File not found");


auto size = getSize(name);
auto size = getSize(name);
if (size <= newSize)
if (size <= newSize)
throw new Exception("new size must be smaller than original size");
throw new Exception("New size must be smaller than original size");


auto content = cast(ubyte[]) read(name, newSize);
auto content = cast(ubyte[]) read(name, newSize);
if (content.length != newSize)
if (content.length != newSize)
throw new Exception("reading file failed");
throw new Exception("Reading file failed");


write(name, content);
write(name, content);