3

In Node.js, is there any way of getting the current offset of a file descriptor or stream? Presently, it is possible to set the offset of a file descriptor, but there seems to be no way to get it.

In C, getting the offset of a file stream is done via ftell, and a file descriptor via lseek(fd, 0, SEEK_CUR).

Example

If a Node.js program needs to check whether there is prior data in a file after opening it in append mode, a call to ftell would help in this case. This can be done in C as follows:

#include <stdio.h>

int main() {
    FILE* f = fopen("test.txt", "a");
    fprintf(f, ftell(f) ? "Subsequent line\n" : "First line\n");
    fclose(f);
}

Running the above program three times, test.txt becomes:

First line
Subsequent line
Subsequent line

Prior Art

mxxk
  • 9,514
  • 5
  • 38
  • 46
  • 1
    There is no equivalent to `ftell()` in node.js. The general idea is that you're either appending or you're specifying the `position` argument with `fs.read()` or `fs.write()`. If you want to just write a bunch of data sequentially, then you use a stream. You can, of course, fetch the current file length and then use position to read some number of bytes from the end of the file if that's what you're really trying to do. Or just use the current file length to know if you're at the beginning of the file after opening it for appending. – jfriend00 Feb 15 '20 at 10:30

2 Answers2

2

There is no equivalent to ftell() or fseek() in node.js and I'm not really sure why. Instead, you generally specify the position you want to read or write at whenever you read or write with fs.read() or fs.write(). If you want to just write a bunch of data sequentially or you want buffered writing, then you would more typically use a stream which buffers and sequences for you.

Instead, if you want to know where data will be appended, you can fetch the current file length and then use that current file length to know if you're at the beginning of the file after opening it for appending.

Here's node.js code that does something similar to your C code.

const fs = require('fs');

async function myFunc() {
     let handle = await fs.promises.open("test.txt");
     try {
         const {size} = await handle.stat();
         await handle.appendFile(size ? "Subsequent line\n" : "First line\n");
     } finally {
         await handle.close();
     }
}

And, if you call this three times like this:

async function test() {
    await myFunc();
    await myFunc();
    await myFunc();
}

test();

You will get your desired three lines in the file:

First line
Subsequent line
Subsequent line
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @mxxk - Does this answer your question? – jfriend00 Feb 17 '20 at 02:22
  • Yes @jfriend00 thank you. :) At the outset, it seemed like there is no way to accomplish what I asked about in Node.js, but perhaps this would draw the attention of future development and one day this will be possible. – mxxk Feb 19 '20 at 00:35
0

The fs.read has position parameter.

fs.read(fd, buffer, offset, length, position, callback)

The position parameter is important here.

Will this suffice your need unless I am not able to understand your question correctly?

rajesh pillai
  • 8,102
  • 7
  • 43
  • 60