-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
docIssues and PRs related to the documentations.Issues and PRs related to the documentations.fsIssues and PRs related to the fs subsystem / file system.Issues and PRs related to the fs subsystem / file system.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.
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
Metadata
Metadata
Assignees
Labels
docIssues and PRs related to the documentations.Issues and PRs related to the documentations.fsIssues and PRs related to the fs subsystem / file system.Issues and PRs related to the fs subsystem / file system.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.