fdopen - Open file descriptor

fdopen() is a standard input output library function to associate an file descriptor to an FILE pointer. Fopen C stdio call or open() system call can be used to open a file descriptor.

Parameters

Like fopen() it also takes two arguments. First argument is file descriptor. Second argument is the mode. There are largely three modes of opening a file. They are "read" ("r"), "write" ("w") and "append" ("a"). File can be in the form of "text" file or "binary" ("b") file.

Syntax

#include <stdio.h>
FILE * fdopen(int fd, cosnt char * mode);

Return

fdopen returns a pointer to a FILE context structure which contains file descriptor and file buffer details. If there are not enough permissions then fdopen() may fail and a NULL pointer is returned.

Opening file for reading:

Opening an existing file can be done in read mode. Read in text mode "r" or read binary "rb" can be other option
FILE *fp = NULL;
fp = fdopen (fd, "r");

Appending an existing file:

We often require to write some new content at the end of existing file. Append mode is the preferred choice.
FILE *fp = NULL;
fp = fopen (fd, "a");

Creating a new File:

Write mode is used to create a new file. If file is already there the file will be truncated to zero bytes and new content will be overwritten.
FILE *fp = NULL;
Fp = fopen (fd, "w");

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

arithmeic math, trigonometric, fopen, fdopen, write vs append mode, binary vs text mode, binary mode, fseek, ftell, rewind,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan