Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of fd (file descriptor) in Node.js?

Tags:

    fs.open('input.txt', 'r+', function(err, fd) {        console.log(fd);        if (err) {            return console.error(err);        }     }) 

What is fd here, and why is it giving 3 when I print it using console.log();?

like image 492
karthi Avatar asked Apr 21 '16 13:04

karthi


People also ask

What is fd in file system?

In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.

What is the use of file descriptor?

A file descriptor is an unsigned integer used by a process to identify an open file. The number of file descriptors available to a process is limited by the /OPEN_MAX control in the sys/limits. h file. The number of file descriptors is also controlled by the ulimit -n flag.

What is fd in fs read?

Syntax fs.read(fd, buffer, offset, length, position, callback) Parameters: fd: File descriptor returned by fs. open() method.

Why we use fs in node JS?

js provides an inbuilt module called FS (File System). Node. js gives the functionality of file I/O by providing wrappers around the standard POSIX functions. All file system operations can have synchronous and asynchronous forms depending upon user requirements.


1 Answers

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.

In fact stdout, stdin and stderr get assigned a file descriptor too, they are occupying fd 0 through 2, the next free file descriptor value is 3. Thats why the value returned in your example is 3.

See Wikipedia for more background information.

like image 56
Jonas Köritz Avatar answered Oct 24 '22 02:10

Jonas Köritz