FAT | RFS | XFS | ZFS

fdopen()

PROTOTYPE

#include <stdio.h>

FILE *fdopen(int fid, const char *mode);

DESCRIPTION

fdopen() returns a file pointer that references the same file control block as fid. It also clears the file’s error and end-of-file indicators. mode is the same as for fopen() except that file creation and truncation do not apply. It must be one of the following:

  • “r”, “rb” - open an existing file for reading.
  • “w”, “wb” - open an existing file for writing.
  • “a”, “ab” - open an existing file for writing in append mode.
  • “r+”, “r+b”, “rb+” - open an existing file for reading and writing.
  • “w+”, “w+b”, “wb+” - open an existing file for reading and writing.
  • “a+”, “a+b”, “ab+” - open an existing file for reading and writing in append mode.

mode must be compatible with how the file was opened. If mode allows reading, the file must be open in read-only or read-write mode. If mode allows writing, the file must be open in write-only or read-write mode. If mode specifies append mode, the file must be open in append mode.

After fdopen() is called, some file systems require fclose() to be used when the file is closed, not close(). Blunk’s file systems don’t have that restriction. fclose() may be used with the file pointer or close() may be used with the equivalent identifier.

If successful, fdopen() returns a stream handle. Otherwise, it sets errno and returns NULL.

ERROR CODES

EBADF fid is not the descriptor of an open file.
EFAULT mode equals NULL.
EINVAL mode is invalid.

EXAMPLE

    fid = open("dac_rslt.txt", O_WRONLY);
    if (fid < 0)
      error("open() failed");
    ftell_val = ftell(fdopen(fid, "w"));
    if (ftell_val != buf_sz)
      ferr = TRUE;