How to use the ftruncate function from fs
Find comprehensive JavaScript fs.ftruncate code examples handpicked from public code repositorys.
GitHub: wisnuc/appifi
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 {
28
63
0
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
42
19
0
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);
9
15
0
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'); }); }
0
0
0
+ 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'))); })); }
0
0
0
+ 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) => {
0
0
0
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) {
0
0
0
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){
0
0
0
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) => {
0
0
0
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) => {
0
0
0
fs.readFileSync is the most popular function in fs (2736 examples)