How to use the ftruncate function from fs

Find comprehensive JavaScript fs.ftruncate code examples handpicked from public code repositorys.

27
28
29
30
31
32
33
34
35
36

ws.on('finish', () => {
  if (error) return
  fs.open(filePath, 'r+', (err, fd) => {
    if (err) return callback(err)
    fs.ftruncate(fd, size, err => {
      fs.close(fd, x => x)
      if (err) {
        return callback(err)
      } else {
fork icon28
star icon63
watch icon0

538
539
540
541
542
543
544
545
546
547
  }
  return true;
};

common.runWithInvalidFD((fd) => {
  fs.ftruncate(fd, 4, common.mustCall(validateError));

  assert.throws(
    () => fs.ftruncateSync(fd, 4),
    validateError
fork icon42
star icon19
watch icon0

110
111
112
113
114
115
116
117
118
119
if (er) return cb(er);
assert.equal(stat.size, 1024 * 16);

fs.open(filename, 'w', function(er, fd) {
  if (er) return cb(er);
  fs.ftruncate(fd, 1024, function(er) {
    if (er) return cb(er);
    fs.stat(filename, function(er, stat) {
      if (er) return cb(er);
      assert.equal(stat.size, 1024);
fork icon9
star icon15
watch icon0

105
106
107
108
109
110
111
112
113
114
115


function ftruncate() {
  const fs = require('fs');
  fs.writeFileSync('fs10.txt', '123', 'utf8');
  const fd = fs.openSync('fs10.txt', 'r+');
  fs.ftruncate(fd, 1, () => {
    fs.unlinkSync('fs10.txt');
  });
}

fork icon0
star icon0
watch icon0

+ 2 other calls in file

161
162
163
164
165
166
167
168
169
170
{
  const file4 = path.resolve(tmp, 'truncate-file-4.txt');
  fs.writeFileSync(file4, 'Hi');
  const fd = fs.openSync(file4, 'r+');
  process.on('beforeExit', () => fs.closeSync(fd));
  fs.ftruncate(fd, 4, common.mustSucceed(() => {
    assert(fs.readFileSync(file4).equals(Buffer.from('Hi\u0000\u0000')));
  }));
}

fork icon0
star icon0
watch icon0

+ 20 other calls in file

36
37
38
39
40
41
42
43
44
45
46


lib.update = (dir, file, data, callback) => {
    fs.open(lib.basedir+dir+'/'+file+'.json','r+', (openError, fileDescriptor) => {
        if (!openError && fileDescriptor){
            const stringData = JSON.stringify(data);
            fs.ftruncate(fileDescriptor,(truncateError) => {
                if (!truncateError){
                    fs.writeFile(fileDescriptor, stringData, (writeError) => {
                       if (!writeError){
                           fs.close(fileDescriptor, (closeError) => {
fork icon0
star icon0
watch icon0

62
63
64
65
66
67
68
69
70
71
if (!err && fileDescriptor) {
  // Convert data to string
  var stringData = JSON.stringify(data);

  // Truncate the file
  fs.ftruncate(fileDescriptor, function(err) {
    if (!err) {
      // Write to file and close it
      fs.writeFile(fileDescriptor, stringData, function(err) {
        if (!err) {
fork icon0
star icon0
watch icon0

50
51
52
53
54
55
56
57
58
59
if(!err && fileDescriptor){
    // convert the data to string 
    const stringData = JSON.stringify(data);

    // truncate the file 
    fs.ftruncate(fileDescriptor, (err)=>{
        if(!err){
            // write to file and close it 
            fs.writeFile(fileDescriptor, stringData, (err)=>{
                if(!err){
fork icon0
star icon0
watch icon0

43
44
45
46
47
48
49
50
51
52
fs.open(`${lib.baseDir}${dir}/${file}.json`, 'r+', (err, fileDescriptor) => {
  if (!err && fileDescriptor) {
    const stringData = JSON.stringify(data);

    // Truncate the file
    fs.ftruncate(fileDescriptor, (err) => {
      if (!err) {
        fs.writeFile(fileDescriptor, stringData, (err) => {
          if (!err) {
            fs.close(fileDescriptor, (err) => {
fork icon0
star icon0
watch icon0

8
9
10
11
12
13
14
15
16
17
18
});


fs.open('file1.txt', 'r+', (err, fd) => {
    if (err) throw err;
    console.log('File opened');
    fs.ftruncate(fd, 7, (err) => {
        if (err) throw err;
        console.log('File turncated after 7 bytes');
        console.log('Reading the same file');
        fs.readFile(fd, (err, data) => {
fork icon0
star icon0
watch icon0