Nikita
Nikita

Reputation: 33

How to use fs.write function of node.js?

I am using node.js for the first time. I want to write a file using the fs.write function. I have taken reference from its file system docs. Its syntax is like this: fs.write(fd, buffer, offset, length, position, callback)

I know how to use fd, buffer and callback in this, but I am not able to figure out how to pass offset, length and position.

Should they be some integers or string?... or what? I am not able to find out.

Upvotes: 3

Views: 3694

Answers (1)

Scimonster
Scimonster

Reputation: 33399

offset and length are integers referring to positions of the Buffer. offset is where in the Buffer should we write from; length is how many bytes should be written.

So, as an example, if you had a Buffer with data abcdefghijklmnopqrstuvwxyz, you could write cdef with offset: 2, length: 4.

position is an integer for where in the file the data should be written. So, if you have an existing file, you could overwrite part of it by setting the position to something between the beginning and the end.

Upvotes: 1

Related Questions