I am trying to use the function ftruncate()
to truncate a file.
I know from looking online that it goes like this:
int ftruncate(int fildes, off_t length);
But I don't know what to supply for the arguments int fd
and off_t length
.
I am trying to use the function ftruncate()
to truncate a file.
I know from looking online that it goes like this:
int ftruncate(int fildes, off_t length);
But I don't know what to supply for the arguments int fd
and off_t length
.
The first argument is the file descriptor. This is the handle to the file that you get from functions like open(3)
or creat(3)
. The second argument is the size you want to truncate the file to. Use 0
if you want to truncate it completely. Finally, the returned value is 0
if the operation was successful and -1
otherwise (and errno(3)
is set accordingly). For more information, see ftruncate(3)
.
However, it might be the case that you are confused because you are trying to use a POSIX function instead of C or C++ standard ones. If so, check out How to truncate a file in C? and similar questions for solutions that rely only on the C or C++ standards.