-3

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
joe
  • 19
  • 3
  • Show what you have attempted and Ask a question with a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve) – Vishaal Shankar Mar 26 '18 at 10:14
  • @VishaalShankar: he seems to be asking how the function is used, rather than having a run-time problem. – Acorn Mar 26 '18 at 10:52

1 Answers1

1

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.

Acorn
  • 24,970
  • 5
  • 40
  • 69