@@ -739,19 +739,59 @@ added: v0.8.6
739
739
-->
740
740
741
741
* ` fd ` {Integer}
742
- * ` len ` {Integer}
742
+ * ` len ` {Integer} default = ` 0 `
743
743
* ` callback ` {Function}
744
744
745
745
Asynchronous ftruncate(2). No arguments other than a possible exception are
746
746
given to the completion callback.
747
747
748
+ If the file referred to by the file descriptor was larger than ` len ` bytes, only
749
+ the first ` len ` bytes will be retained in the file.
750
+
751
+ For example, the following program retains only the first four bytes of the file
752
+
753
+ ``` js
754
+ console .log (fs .readFileSync (' temp.txt' , ' utf8' );
755
+ // prints Node.js
756
+
757
+ // get the file descriptor of the file to be truncated
758
+ const fd = fs .openSync (' temp.txt' , ' r+' );
759
+
760
+ // truncate the file to first four bytes
761
+ fs .ftruncate (fd, 4 , (err ) => {
762
+ assert .ifError (err);
763
+ console .log (fs .readFileSync (' temp.txt' , ' utf8' ));
764
+ });
765
+ // prints Node
766
+ ` ` `
767
+
768
+ If the file previously was shorter than ` len` bytes, it is extended, and the
769
+ extended part is filled with null bytes ('\0 '). For example,
770
+
771
+ ` ` ` js
772
+ fs .readFileSync (' temp.txt' , ' utf-8' );
773
+ // prints Node.js
774
+
775
+ // get the file descriptor of the file to be truncated
776
+ const fd = fs .openSync (' temp.txt' , ' r+' );
777
+
778
+ // truncate the file to 10 bytes, whereas the actual size is 7 bytes
779
+ fs .ftruncate (fd, 10 , (err ) => {
780
+ assert .ifError (! err);
781
+ console .log (fs .readFileSync (' temp.txt' ));
782
+ });
783
+ // prints <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>
784
+ ` ` `
785
+
786
+ The last three bytes are zeroes, to compensate the over-truncation.
787
+
748
788
## fs.ftruncateSync(fd, len)
749
789
<!-- YAML
750
790
added: v0.8.6
751
791
-->
752
792
753
793
* ` fd` {Integer}
754
- * ` len ` {Integer}
794
+ * ` len` {Integer} default = ` 0 `
755
795
756
796
Synchronous ftruncate(2). Returns ` undefined ` .
757
797
@@ -1368,7 +1408,7 @@ added: v0.8.6
1368
1408
-->
1369
1409
1370
1410
* ` path` {String | Buffer}
1371
- * ` len ` {Integer}
1411
+ * ` len` {Integer} default = ` 0 `
1372
1412
* ` callback` {Function}
1373
1413
1374
1414
Asynchronous truncate(2). No arguments other than a possible exception are
@@ -1381,9 +1421,10 @@ added: v0.8.6
1381
1421
-->
1382
1422
1383
1423
* ` path` {String | Buffer}
1384
- * ` len ` {Integer}
1424
+ * ` len` {Integer} default = ` 0 `
1385
1425
1386
- Synchronous truncate(2). Returns ` undefined ` .
1426
+ Synchronous truncate(2). Returns ` undefined ` . A file descriptor can also be
1427
+ passed as the first argument. In this case, ` fs .ftruncateSync ()` is called.
1387
1428
1388
1429
## fs.unlink(path, callback)
1389
1430
<!-- YAML
0 commit comments