Node.js fs.ftruncate() Method
The fs.ftruncate() method is used to change the size of the file i.e. either increase or decrease the file size. It changes the length of the file at the path by len bytes. If len is shorter than the file’s current length, the file is truncated to that length. If it is greater than the file length, it is padded by appending null bytes (x00) until len is reached. It is similar to the truncate() method, except it accepts a file descriptor of the file to truncate.
fs.ftruncate(fd, len, callback)
Parameters: This method accept three parameters as mentioned above and described below:
- fd: It is an integer value which denotes the file descriptor of the file to truncate.
- len: It is an integer value which specifies the length of the file after which the file will be truncated. It is an optional parameter. The default value is 0, which means that the whole file would be truncated.
- callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
Below examples illustrate the fs.ftruncate() method in Node.js:
Contents of file before truncate: This is an example file for the ftruncate() method. Contents of file after truncate: This is an example file
Contents of file before truncate: This is an example file for the ftruncate() method. Contents of file after truncate:
My Personal Notes arrow_drop_up
Please Login to comment...