Skip to content
This repository

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

Closed
wants to merge 1 commit into from

6 participants

Hector Ben Noordhuis Nodejs Jenkins David Dias Trevor Norris Timothy J Fontaine
Hector

This example shows to how to use fs.read() with a buffer.

It also shows how to use fs.open() and fs.close()

Ben Noordhuis bnoordhuis commented on the diff
doc/api/fs.markdown
@@ -341,6 +341,36 @@ If `position` is `null`, data will be read from the current file position.
341 341
342 342 The callback is given the three arguments, `(err, bytesRead, buffer)`.
343 343
  344 + var fs = require('fs');
  345 + var fileName = __dirname + '/test.txt';
  346 +
  347 + // Create the test file (this is sync on purpose)
  348 + fs.writeFileSync(fileName, 'five guys named moe', 'utf8');
  349 +
  350 + // Read async
  351 + fs.open(fileName, 'r', function(err, fd) {
  352 + var content = new Buffer("123456789", "utf8");
  353 + if(err) {
1
Ben Noordhuis
bnoordhuis added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Ben Noordhuis

Hector, can you sign the CLA?

Nodejs Jenkins
Collaborator

Can one of the admins verify this patch?

David Dias
Collaborator

@hectorcorrea interest in updating the styles and signing the CLA to get this in?

Trevor Norris
Collaborator

It's easy enough to fix the styling issue. Need two things:

1) @hectorcorrea you'll need to sign the CLA
2) Since these are so old, want these examples to be run again to make sure they actually still work.

Timothy J Fontaine
Owner

I'm going to close this, would certainly entertain documentation patches in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 30, 2012
Hector hectorcorrea Included example on how to use fs.read() aef09d4
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 30 additions and 0 deletions. Show diff stats Hide diff stats

  1. +30 0 doc/api/fs.markdown
30 doc/api/fs.markdown
Source Rendered
@@ -341,6 +341,36 @@ If `position` is `null`, data will be read from the current file position.
341 341
342 342 The callback is given the three arguments, `(err, bytesRead, buffer)`.
343 343
  344 + var fs = require('fs');
  345 + var fileName = __dirname + '/test.txt';
  346 +
  347 + // Create the test file (this is sync on purpose)
  348 + fs.writeFileSync(fileName, 'five guys named moe', 'utf8');
  349 +
  350 + // Read async
  351 + fs.open(fileName, 'r', function(err, fd) {
  352 + var content = new Buffer("123456789", "utf8");
  353 + if(err) {
  354 + console.log("Error opening file: ", err);
  355 + } else {
  356 + console.log("Open returned fd: ", fd);
  357 + fs.read(fd, content, 0, 9, 0, function(err, bytesRead, buffer) {
  358 + if(err) {
  359 + console.log("Error reading: ", err);
  360 + } else {
  361 + console.log("# of bytes read: ", bytesRead);
  362 + console.log("bytes: ", buffer);
  363 + console.log("string: [" + buffer.toString() + "]");
  364 + fs.close(fd, function() {
  365 + console.log("fd %d has been closed", fd);
  366 + });
  367 + }
  368 + });
  369 + }
  370 + });
  371 +
  372 +
  373 +
344 374 ## fs.readSync(fd, buffer, offset, length, position)
345 375
346 376 Synchronous version of buffer-based `fs.read`. Returns the number of

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.