Node.js | fs.ftruncateSync() Method

The fs.ftruncateSync() method is used to synchronously 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 truncateSync() method, except it accepts a file descriptor of the file to truncate.

Syntax:

fs.ftruncateSync( fd, len )

Parameters: This method accept two 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.

Below examples illustrate the fs.ftruncateSync() method in Node.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.ftruncateSync() method
  
// Import the filesystem module
const fs = require('fs');
  
console.log("Contents of file before truncate:")
console.log(fs.readFileSync('example_file.txt', 'utf8'));
  
// Get the file descriptor of the file
const fd = fs.openSync('example_file.txt', 'r+');
  
// Truncate the whole file
fs.ftruncateSync(fd);
  
console.log("Contents of file after truncate:")
console.log(fs.readFileSync('example_file.txt', 'utf8'));

chevron_right


Output:

Contents of file before truncate:
This is an example file for the ftruncateSync() method.
Contents of file after truncate:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.ftruncateSync() method
  
// Import the filesystem module
const fs = require('fs');
  
console.log("Contents of file before truncate:")
console.log(fs.readFileSync('example_file.txt', 'utf8'));
  
// Get the file descriptor of the file
const fd = fs.openSync('example_file.txt', 'r+');
  
// Decrease the file size
fs.ftruncateSync(fd, 18);
  
console.log("Contents of file after truncate:")
console.log(fs.readFileSync('example_file.txt', 'utf8'));
  
// Increase the file size
fs.ftruncateSync(fd, 25);
  
console.log("Contents of file in bytes after truncate:")
console.log(fs.readFileSync('example_file.txt'));

chevron_right


Output:

Contents of file before truncate:
This is an example file for the ftruncateSync() method.
Contents of file after truncate:
This is an example
Contents of file in bytes after truncate:

Reference: https://nodejs.org/api/fs.html#fs_fs_ftruncatesync_fd_len




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.