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();
?
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.
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.
Syntax fs.read(fd, buffer, offset, length, position, callback) Parameters: fd: File descriptor returned by fs. open() method.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With