Truncate a file: Difference between revisions
Content added Content deleted
Line 625: | Line 625: | ||
-- @param {string} filename |
-- @param {string} filename |
||
-- @param {integer} length |
-- @param {integer} length |
||
-- @return {bool} |
-- @return {bool} success |
||
---------------------------------------- |
---------------------------------------- |
||
on truncate (filename, length) |
on truncate (filename, length) |
||
fp = xtra("fileIO").new() |
fp = xtra("fileIO").new() |
||
fp.openFile(filename, 0) |
fp.openFile(filename, 0) |
||
⚫ | |||
if fp.status() then return false |
if fp.status() then return false |
||
if fp.getLength()=length then |
|||
-- noting to do |
|||
fp.closeFile() |
|||
⚫ | |||
end if |
|||
⚫ | |||
if data.length<>length then |
|||
fp.closeFile() |
|||
return false |
|||
end if |
|||
fp.delete() |
fp.delete() |
||
fp.createFile(filename) |
fp.createFile(filename) |
||
fp.openFile(filename, 2) |
fp.openFile(filename, 2) |
||
fp.writeByteArray(data) |
fp.writeByteArray(data) |
||
ok = fp.status()=0 |
|||
fp.closeFile() |
fp.closeFile() |
||
return ok |
|||
fp=0 |
|||
⚫ | |||
end</lang> |
end</lang> |
||
But there are also free plugins ("Xtras") like e.g. "BinFile Xtra" that support "in-file" truncation: |
But there are also free plugins ("Xtras") like e.g. "BinFile Xtra" that support "in-file" truncation: |