Truncate a file: Difference between revisions

→‎{{header|C}}: POSIX truncate
m ({{header|ZX Spectrum Basic}})
(→‎{{header|C}}: POSIX truncate)
Line 4:
 
=={{header|C}}==
===Windows===
Windows uses <code>SetEndOfFile()</code> to change the length of a file. This program can truncate or extend a file. It can detect and print errors.
 
Line 92 ⟶ 93:
return dotruncate(fn, fp);
}</lang>
===POSIX===
<lang c>#include <unistd.h>
#include <sys/types.h>
 
...
truncate(filename, length);
ftruncate(fd, length);
...
</lang>
Both functions have <code>length</code> argument of <code>off_t</code> type. There are about a million possible errors, of interest to this task are <code>EFBIG</code>: size too large; <code>EINVAL</code>: size is negative or too large; <code>EIO</code>: IO error; <code>EACCESS</code> (for <code>truncate</code>): either can't see or can't write to the file; <code>EBADF</code> or <code>EINVAL</code> (for <code>ftruncate</code>): file descriptor is not a file descriptor, or not opened for writing.
 
When specifying a new file size larger than current value, the file will be extended and padded with null bytes.
 
==Icon and {{header|Unicon}}==
Anonymous user