Working with File Descriptors

Rubleen Kaur
Last Updated: May 13, 2022

Introduction

File descriptors are a concept used in many programming languages, they represent a reference to an opened file. The file descriptor will be used to reference the correct file stream by all file system-related functions.



For example, 

 

const fs = require('fs')

fs.open('/Users/CodingNinjas/test.txt''r'(err, fd) => {
  //fd is the file descriptor.
})

 

We have used ‘r’ as the second parameter to the fs.open() call. The ‘r’ flag means that we are opening the file for reading.

 

Let’s go through the various other tags available for us to use along with the fs.open() call. 

Available Tags

 

The different available flags used which are used along file descriptors are: 
→ ‘r+’ flag: This flag is used to open the file for reading and writing. If the file is not created, this flag will; they not create a new file.
→ ‘w+’ flag: This flag is used to open the file for reading and writing. It also positions the stream at the beginning of the file.
→ ‘a’ flag: This flag opens the file for writing, and it positions the stream at the end of the file. The file gets created if it does not already exist.
→ ‘a+’ flag: This flag opens the file for both reading and writing, and it positions the stream at the end of the file. The file gets created if it does not already exist.

Let’s consider an example for understanding the concept of file descriptors:

const fs = require('fs');

const str = 'coding ninjas is awesome! \n';
const filename = 'cn.txt';

fs.open(filename, 'a'(err, fd) => {
  if (err) {
    console.log(err.message);
  } else {
    fs.write(fd, str, (err, bytes) => {
      if (err) {
        console.log(err.message);
      } else {
        console.log(bytes + ' bytes written');
      }
    });
  }
  fs.close(fd, (err) => {
    if (err) console.error('failed to close file', err);
    else {
      console.log('\n> file closed');
    }
  });
});


OUTPUT






 

FAQs

  1. What are File Descriptors in NodeJS?
    File descriptors are a concept used in many programming languages, they represent a reference to an opened file. The file descriptor will be used to reference the correct file stream by all file system-related functions.

     
  2. What are the different available flags used along with File Descriptors?
    The different available flags used are: 
    → ‘r+’ flag: This flag is used to open the file for reading and writing, if the file is not created, this flag will not create a new file.
    → ‘w+’ flag: This flag is used to open the file for reading and writing. It also positions the stream at the beginning of the file.
    → ‘a’ flag: This flag opens the file for writing, and it positions the stream at the end of the file. The file gets created if it does not already exist.
    → ‘a+’ flag: This flag opens the file for both reading and writing, and it positions the stream at the end of the file. The file gets created if it does not already exist.


     
  3. What is the use of the ‘w’ flag?
    This flag is used to open the file for reading and writing. It also positions the stream at the beginning of the file.
     

Key Takeaways

Hey everyone, so let’s brief out the article. Let’s discuss in brief whatever we have here. 

  •  File descriptors are a concept used in many programming languages, and they represent a reference to an opened file. The file descriptor will be used to reference the correct file stream by all file system-related functions.

     
  • We have further covered an example to understand the use of file descriptors and the tags we use. In the discussed example, we have used the ‘a’ flag, which opens the file for writing, and positions the stream at the end of the file. The file gets created if it does not already exist.


     

Isn’t Web Development engaging? Building new websites and using amazing animations and different APIs, don’t be scared if you cannot grasp the hold of this vast development. We have the perfect web development course for you to make you stand out from your fellow developers. 

 

Happy Learning Ninjas!

 

Was this article helpful ?
0 upvotes