This repository has been archived by the owner. It is now read-only.

Included example on how to use fs.read() #3190

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+30 −0
Split
View
@@ -341,6 +341,36 @@ If `position` is `null`, data will be read from the current file position.
The callback is given the three arguments, `(err, bytesRead, buffer)`.
+ var fs = require('fs');
+ var fileName = __dirname + '/test.txt';
+
+ // Create the test file (this is sync on purpose)
+ fs.writeFileSync(fileName, 'five guys named moe', 'utf8');
+
+ // Read async
+ fs.open(fileName, 'r', function(err, fd) {
+ var content = new Buffer("123456789", "utf8");
+ if(err) {
@bnoordhuis

bnoordhuis Apr 30, 2012

Member

Style issue: space between if and parenthesis. Happens again a few lines below.

Another style issue is that code examples should use two space indent.

+ console.log("Error opening file: ", err);
+ } else {
+ console.log("Open returned fd: ", fd);
+ fs.read(fd, content, 0, 9, 0, function(err, bytesRead, buffer) {
+ if(err) {
+ console.log("Error reading: ", err);
+ } else {
+ console.log("# of bytes read: ", bytesRead);
+ console.log("bytes: ", buffer);
+ console.log("string: [" + buffer.toString() + "]");
+ fs.close(fd, function() {
+ console.log("fd %d has been closed", fd);
+ });
+ }
+ });
+ }
+ });
+
+
+
## fs.readSync(fd, buffer, offset, length, position)
Synchronous version of buffer-based `fs.read`. Returns the number of