在C中移动不带fseek的文件位置



如何将文件位置移回C中的x个字节?

我不想使用fseek(),因为我正在使用open(fd, buf, buflength)而不是fopen()读取文件的行。

我非常希望避免将整个文件读入内存。

我想做的基本上是:

  1. 读取X+A=Y字节
  2. 对前X个字节执行某些操作
  3. 将文件指针向后移动A字节
  4. 从中读取Y字节

除了第三步,我什么都知道怎么做。有人知道怎么做吗?

根据http://rabbit.eng.miami.edu/info/functions/unixio.html你可以使用lseek:

int lseek(int fd, int position, int startpoint)
  include: <unistd.h>
  Sets the file position effective for the next read or write operation.
  fd = file descriptor as returned by open
  position = position within file:
    number of bytes between startpoint and the desired position, may be negative.
  startpoint = location in file that position is relative to, one of:
    SEEK_SET        Position is number of bytes from beginning of file
    SEEK_END        Position is number of bytes after end of file
    SEEK_CUR        Position is number of bytes after current position
  returns <0 for error,
    or resulting file position relative to beginning of file.
  Moving beyond the end of the file is permitted; the file is extended in length
  only if a write occurs beyond the current end of file. Moving before the
  beginning of a file is not permitted. A write operation after a move only
  overwrites the number of bytes actually written.

相关内容

  • 没有找到相关文章

最新更新