Closed
Description
- Version: 6.2.0
- Platform: Windows 7
- Subsystem: doc, fs
Sometimes I use the same file descriptor for writing and then reading by readline
(possible case: write some data line by line, then reread it into an array to sort and rewrite).
It seems fs
doc lacks for some point concerning these cases.
fs.createReadStream(path[, options]):
options
can includestart
andend
values to read a range of bytes from the file instead of the entire file. Bothstart
andend
are inclusive and start at 0.
Maybe it should be mentioned that start
is set by default to the file end (or to the write position) for fd that has been previously written into.
Test:
const fs = require('fs');
const rl = require('readline');
const file = fs.openSync('test.txt', 'w+');
fs.writeSync(file, '1\n2\n3\n');
rl.createInterface({
input: fs.createReadStream(null, {fd: file})
}).on('line', line => {
console.log(line);
}).on('close', () => {
console.log('Closed');
});
The output is only
Closed
const fs = require('fs');
const rl = require('readline');
const file = fs.openSync('test.txt', 'w+');
fs.writeSync(file, '1\n2\n3\n');
rl.createInterface({
input: fs.createReadStream(null, {fd: file, start: 0})
}).on('line', line => {
console.log(line);
}).on('close', () => {
console.log('Closed');
});
The output contains all the file:
1
2
3
Closed