Release 1

This article focuses on using the fs.open method in Node.js. This article describes the

fs.open method description, syntax, receiving parameters, usage examples, and

implementation source code.

Method description:

Open the file asynchronously.

In POSIX systems, the path is considered to exist by default (even if the file in the path does not exist)

The logo ID may or may not run under the network file system.

grammar:

fs .openpathflags[mode][callback(err,fd)]

Since this method belongs to the fs module, you need to introduce the fs module before use (var fs = require(“fs”))

Receive parameters:

Path file path

The flag can be the following value

Copy the code as follows:

'r' - Open the file in read mode.
'r+' - Open the file in read-write mode.
'rs' - Open and read files using synchronous mode. Instructs the operating system to ignore the local file system cache.
'rs+' - Opens, reads and writes files in a synchronous manner.
Note: This is not a blocking operation that causes fs.open to become synchronized. If you want to synchronize mode, use fs.openSync().
'w' - open the file in read mode, create if the file does not exist
'wx' - the same as 'w' mode, returning failure if file exists
'w+' - open the file in read-write mode, create if the file does not exist
'wx+'- Same as 'w +' mode, returning failure if file exists
'a'- open file in append mode, create if file does not exist
'ax' - the same as 'a' mode, returning failure if file exists
' a +' - open the file in read add mode, create if the file does not exist
'ax +' - same as 'a +' mode if the file exists
The return failure mode is used to create file permissions when creating a file. Default 0666

 

Callback callback function passes file descriptor fd and exception error

Example: Copy the code as follows:

var fs = require('fs');

fs.open('/path/demo1.txt', 'a', function (err, fd) {

  if (err) {

    throw err;

  }

  fs.futimes(fd, 1388648322, 1388648322, function (err) {

    if (err) {

      throw err;

    }

    console.log('futimes complete');

    fs.close(fd, function () {

      console.log('complete'); 

    }); 

  }); 

});

Source code:

Copy the code as follows:

fs.open = functionpath,flags,mode,callback { 

  callback = makeCallback(arguments [ arguments .length  - 1 ]); 

  mode = modeNum(mode,438  / * = 0666 * /); if(!nullCheck(path,callback))返回 ; 
  binding.open(pathModule._makeLong(path),
               stringToFlags(flags),
               mode,
               callback); 
};

  





At the same time, I want to test if I can pass ‘r‘ in the file after replacing ‘r + ‘.
Unfortunately, I did not pass the test. I think if the file path is incorrect, the test won’t
run. Later, I tried again, but unfortunately, I failed the test again.
Although the test did not pass, I found the file documentation found (here) read, I
learned some usage of  fs.opne in node.js. And by being “Slack” with everyone, try to
master the knowledge you  don’t understand and enrich yourself. Let me become more
“powerful”.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s