I am trying to write some meta information about a file by using 1024 bytes at the end of the file.
var buffer = new Buffer(1024);
buffer.write(JSON.stringify(data));
Once the string is copies to the buffer I am saving the file like this -
fs.write(fd, buffer, 0, buffer.length, fileSize,callback);
When I read the file I just want to extract the meta information saved in the data variable. I am doing this like this -
buffer = new Buffer(1024);
fs.read(fd, buffer, 0, buffer.length, fileSize, function(err, count, buffer) {
console.log(buffer.toJSON())
}
Unfortunately the above procedure gets the json data but has some garbage data surrounding it. buffer.toJSON() method thus fails. What should be my approach for such a problem?
Edit: The buffer does not get completely filled.
Current Solution: 1. Fill the Buffer with null. 2. Use JSON5 to parse the json content.
Final Solution 1. Fill the Buffer with ' ' 2. Use JSON parse to parse Json Content