Skip to content

Instantly share code, notes, and snippets.

@markstory
Forked from chartjes/gist:1711104
Created January 31, 2012 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstory/1711119 to your computer and use it in GitHub Desktop.
Save markstory/1711119 to your computer and use it in GitHub Desktop.
Hands On Node File System Exercise #3
var fs = require('fs');
function readFiveBytes(filePosition) {
var readFile = function (err, fd) {
if (err) {
console.log(err.message);
return;
}
var readBuffer = new Buffer(5);
var bufferOffset = 0;
var bufferLength = readBuffer.length;
fs.read(fd, readBuffer, bufferOffset, bufferLength, filePosition, function(err, readBytes) {
if (err) {
throw err;
}
if (readBytes > 0) {
console.log('read one');
console.log(readBuffer.slice(0, readBytes).toString());
}
});
};
return readFile;
}
fs.open('./a.txt', 'r', readFiveBytes(5));
fs.open('./a.txt', 'r', readFiveBytes(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment