UNIXシステムコール 位置決めをしてファイルを読む

#include <sys/types.h>  /* lseek, open, read, write */
#include <stdio.h>      /* fflush, perror, printf, putchar */
#include <stdlib.h>     /* exit */
#include <sys/stat.h>   /* open */
#include <fcntl.h>      /* open */
#include <sys/uio.h>    /* read, write */
#include <unistd.h>     /* close, lseek, read, write */

int main(void) {
  int d, cc;
  char buf[3];
  off_t pos;

  if ((d = open("input.file", O_RDONLY)) == -1) {
    perror("open");
    exit(1);
  }

  if ((pos = lseek(d, 1, SEEK_SET)) == -1) {
    perror("lseek");
    exit(1);
  }

  printf("new offset = %d\n", (int)pos);

  if ((cc = read(d, buf, sizeof(buf))) == -1) {
    perror("read");
    exit(1);
  }

  if ((pos = lseek(d, 0, SEEK_CUR)) == -1) {
    perror("lseek");
    exit(1);
  }

  if (close(d) == -1) {
    perror("close");
    exit(1);
  }

  printf("offset after read = %d\n", (int)pos);
  printf("number of bytes read = %d\n", cc);
  fflush(stdout); /* 画面を崩さないため */
  if (cc > 0) {
    write(STDOUT_FILENO, buf, cc);
  }
  putchar('\n');

  return 0;
}