Created
January 31, 2012 15:34
-
-
Save chartjes/1711104 to your computer and use it in GitHub Desktop.
Hands On Node File System Exercise #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
function readBytes(filePosition, length) { | |
var readFile = function(err, fd) { | |
if (err) { | |
console.log(err.message); | |
return; | |
} | |
var readBuffer = new Buffer(length); | |
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(readBuffer.slice(0, readBytes).toString()); | |
} | |
}); | |
}; | |
return readFile; | |
} | |
fs.open('./a.txt', 'r', readBytes(5, 5)); | |
fs.open('./a.txt', 'r', readBytes(10, 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment