The issue is that the outer ReadStream
will close the fd
after it is used, so reusing it on the second ReadStream
will fail. The newest Node unstable actually has an autoClose
options for ReadStreams
but that is not part of stable yet.
The real answer that is that the information given to you in your previous question is incorrect. createReadStream
is implemented using all public APIs, so there is nothing that it can do that you can't do too. In this case, you can just use fs.read
with its position
argument.
var fs = require('fs');
fs.open('test.txt', 'r', function (err, fd) {
if (err) {
console.error('error opening file: ' + err);
} else {
fs.read(fd, new Buffer(4), 0, 4, 2, function(err, bytesRead, data){
if (err) throw err;
console.log('outer data', data);
fs.read(fd, new Buffer(3), 0, 3, 0, function(err, bytesRead, data2){
if (err) throw err;
console.log('inner data', data2);
fs.close(fd);
// more code to execute after both reads
});
});
}
});