Truncate a file: Difference between revisions
Content deleted Content added
Added Lua version |
|||
Line 550: | Line 550: | ||
</lang> |
</lang> |
||
=={{header|Lua}}== |
|||
Lua strings treat each byte as being invariably one whole character (hence the awkwardness of trying to use it with unicode), so it's safe to use string methods to truncate binary data. |
|||
<lang Lua>function truncate (filename, length) |
|||
local inFile = io.open(filename, 'r') |
|||
if not inFile then |
|||
error("Specified filename does not exist") |
|||
end |
|||
local wholeFile = inFile:read("*all") |
|||
inFile:close() |
|||
if length >= wholeFile:len() then |
|||
error("Provided length is not less than current file length") |
|||
end |
|||
local outFile = io.open(filename, 'w') |
|||
outFile:write(wholeFile:sub(1, length)) |
|||
outFile:close() |
|||
end</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
<lang Mathematica>Truncate[file_, n_] := Module[{filename = file, nbbytes = n, temp}, |
<lang Mathematica>Truncate[file_, n_] := Module[{filename = file, nbbytes = n, temp}, |