Repository navigation
Closed
Labels
docIssues and PRs related to Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.good first issueIssues 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]):
optionscan includestartandendvalues to read a range of bytes from the file instead of the entire file. Bothstartandendare 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 Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.good first issueIssues that are suitable for first-time contributors.
Activity
vsemozhetbyt commentedon Jun 2, 2016
Is
armlabel added by mistake?bnoordhuis commentedon Jun 2, 2016
I think that should be understood as, when set, they start counting from zero, not one.
I agree. @nodejs/documentation: when
fdis specified andstartis omitted or undefined,fs.createReadStream()reads sequentially from the current file position.startcorresponds to thepositionargument tofs.read(), which is documented to behave that way.Mixing sequential reads and writes with concurrent asynchronous file operations is usually unwise because there is no telling in which order they run. Not an issue in the OP's example but replacing
fs.writeSync()withfs.write(), for example, would introduce randomness.WesTyler commentedon Dec 1, 2016
@bnoordhuis / @nodejs/documentation: before I commit/PR, can I get feedback on wording updates? How's this?
Updated wording below:
optionscan includestartandendvalues to read a range of bytes fromthe file instead of the entire file. Both
startandendare inclusive andstart counting at 0. If
fdis specified andstartis omitted or undefined,fs.createReadStream()reads sequentially from the current file position.The
encodingcan be any one of those accepted by [Buffer][].bnoordhuis commentedon Dec 1, 2016
@WesTyler Sounds good to me.
doc: clarify fs.createReadStream options
doc: clarify fs.createReadStream options
doc: clarify fs.createReadStream options
doc: clarify fs.createReadStream options
doc: clarify fs.createReadStream options
doc: clarify fs.createReadStream options
doc: clarify fs.createReadStream options