1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const filename = path.join(common.tmpDir, 'write.txt');
common.refreshTmpDir();
// fs.writeSync with all parameters provided:
{
const fd = fs.openSync(filename, 'w');
let written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár'));
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}
// fs.writeSync with a buffer, without the length parameter:
{
const fd = fs.openSync(filename, 'w');
let written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, Buffer.from('bár'), 0);
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}
// fs.writeSync with a buffer, without the offset and length parameters:
{
const fd = fs.openSync(filename, 'w');
let written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, Buffer.from('bár'));
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}
|