Below is an example of a lower level: fs.read (fd, buffer, offset, length, position, callback)
using:
const fs = require('fs');
const fd = fs.openSync('your-file.txt','r');
function readOneCharFromFile(position, cb){
const b = new Buffer(1);
fs.read(fd, b, 0, 1, position, function(err,bytesRead, buffer){
console.log('data => ', String(buffer));
cb(err,buffer);
});
}
you will need to increase the position when you read the file, but it will work.
here's a quick example of how to read the entire file, character by character
script, ,
const async = require('async');
const fs = require('fs');
const path = require('path');
function read(fd, position, cb) {
let isByteRead = null;
let ret = new Buffer(0);
async.whilst(
function () {
return isByteRead !== false;
},
function (cb) {
readOneCharFromFile(fd, position++, function (err, bytesRead, buffer) {
if(err){
return cb(err);
}
isByteRead = !!bytesRead;
if(isByteRead){
ret = Buffer.concat([ret,buffer]);
}
cb(null);
});
},
function (err) {
cb(err, ret);
}
);
}
function readOneCharFromFile(fd, position, cb) {
const b = new Buffer(1);
fs.read(fd, b, 0, 1, position, cb);
}
const file = path.resolve(__dirname + '/fixtures/abc.txt');
const fd = fs.openSync(file, 'r');
read(fd, 0, function (err, data) {
if (err) {
console.error(err.stack || err);
}
else {
console.log('data => ', String(data));
}
fs.closeSync(fd);
});
, , . , OS , . async.whilst() , , (ret isByteRead). , , .