如何获取流或文件描述符的当前偏移量? [英] How to get current offset of stream or file descriptor?

查看:156
本文介绍了如何获取流或文件描述符的当前偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node.js中,是否有任何方法获取文件描述符或流的当前偏移量?目前,可以设置文件描述符的偏移量,但是似乎没有办法.

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.

在C语言中,文件流的偏移量是通过 ftell ,并通过 lseek(fd,0,SEEK_CUR) .

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

如果Node.js程序在追加模式下打开文件后需要检查文件中是否有先前数据,则在这种情况下,调用 ftell 会有所帮助.可以在C语言中完成以下操作:

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);
}

运行上述程序3次, test.txt 变为:

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

First line
Subsequent line
Subsequent line

现有技术

  • 针对旧版本的node的GitHub问题已提交.讨论中提到了 bytesWritten bytesEmitted ,它们均不等同于 ftell (在上面的C示例中,是文件首次写入时写入的字节).打开始终为0). https://github.com/nodejs/node-v0.x-存档/问题/1527 .
  • fs-ext NPM软件包.公开用于Node.js的底层 lseek . https://www.npmjs.com/package/fs-ext .
  • Prior Art

    • GitHub issue filed for old version of node. The discussion mentions bytesWritten and bytesEmitted, neither of which is equivalent to ftell (in the above C example, the bytes written when the file is first opened is always 0). https://github.com/nodejs/node-v0.x-archive/issues/1527.
    • The fs-ext NPM package. Exposes the low-level lseek for use in Node.js. https://www.npmjs.com/package/fs-ext.
    • 推荐答案

      node.js中没有等效于 ftell() fseek()的代码,我不太确定为什么.而是通常在每次使用 fs.read() fs.write()进行读写时指定要读取或写入的位置.如果您只想顺序写入一堆数据或要进行缓冲写入,那么通常会使用为您缓冲和排序的流.

      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.

      这是与您的C代码相似的node.js代码.

      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
      

      这篇关于如何获取流或文件描述符的当前偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆