C Programming Code Examples
Learn C Language
fdopen() Function in C Programming Language
fdopen() Function in C
Associate a stream with a file descriptor. The fdopen() function associates an input or output stream with the file that is identified by fildes. The mode variable is a character string specifying the type of access that is requested for the stream. The variable contains one positional parameter that is followed by optional keyword parameters.
Syntax for fdopen() Function in C
#include <stdio.h>
FILE *fdopen(int fildes, const char *mode);
fildes
The file descriptor that you want to associate with a stream.
mode
The mode specified when filedes was originally opened. For information, see fopen(), except modes begining with w don't cause truncation of the file.
The possible values for the positional parameters are:
r
Create a stream to read a text file. The file pointer is set to the beginning of the file.
w
Create a stream to write to a text file. The file pointer is set to the beginning of the file.
a
Create a stream to write, in append mode, at the end of the text file. The file pointer is set to the end of the file.
r+
Create a stream for reading and writing a text file. The file pointer is set to the beginning of the file.
w+
Create a stream for reading and writing a text file. The file pointer is set to the beginning of the file.
a+
Create a stream for reading or writing, in append mode, at the end of the text file. The file pointer is set to the end of the file.
rb
Create a stream to read a binary file. The file pointer is set to the beginning of the file.
wb
Create a stream to write to a binary file. The file pointer is set to the beginning of the file.
ab
Create a stream to write to a binary file in append mode. The file pointer is set to the end of the file.
r+b or rb+
Create a stream for reading and writing a binary file. The file pointer is set to the beginning of the file.
w+b or wb+
Create a stream for reading and writing a binary file. The file pointer is set to the beginning of the file.
a+b or ab+
Create a stream for reading and writing to a binary file in append mode. The file pointer is set to the end of the file.
Use the w, w+, wb, wb+, and w+b modes with care; they can destroy existing files.
The specified type must be compatible with the access method you used to open the file. If the file was opened with the O_APPEND flag, the stream mode must be a, a+, ab, a+b, or ab+. To use the fdopen() function you need a file descriptor. To get a descriptor use the POSIX function open(). The O_APPEND flag is a mode for open(). Modes for open() are defined in QSYSINC/H/FCNTL. For further information see the APIs topic in the Information Center.
The keyword parameters allowed for fdopen() are the same as those documented in fopen() - Open Files that are for the integrated file system.
If fdopen() returns NULL, use close() to close the file. If fdopen() is successful, you must use fclose() to close the stream and file.
A file stream for success, or NULL if an error occurs (errno is set).
The filedes argument is a file descriptor that was returned by one of accept(), creat(), dup(), dup2(), fcntl(), open(), pipe(), or sopen(). The fdopen() function preserves the offset maximum previously set for the open file description corresponding to filedes.
The fdopen() function may fail if:
EBADF
The fildes argument is not a valid file descriptor.
EINVAL
The mode argument is not a valid mode.
EMFILE
{FOPEN_MAX} streams are currently open in the calling process.
EMFILE
{STREAM_MAX} streams are currently open in the calling process.
ENOMEM
Insufficient space to allocate a buffer.
/* associates stream with file descriptor by fdopen() function code example */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main(void)
{
long length;
int fh;
char buffer[20];
FILE *fp;
printf("\nCreating sample.dat.\n");
if ((fp= fopen("/sample.dat", "w")) == NULL) {
perror(" File was not created: ");
exit(1);
}
fputs("Sample Program", fp);
fclose(fp);
memset(buffer, '\0', 20); /* Initialize buffer*/
if (-1 == (fh = open("/sample.dat", O_RDWR|O_APPEND))) {
perror("Unable to open sample.dat");
exit(1);
}
if (NULL == (fp = fdopen(fh, "r"))) {
perror("fdopen failed");
close(fh);
exit(1);
}
if (14 != fread(buffer, 1, 14, fp)) {
perror("fread failed");
fclose(fp);
exit(1);
}
printf("Successfully read from the stream the following:\n%s.\n", buffer);
fclose(fp);
return 1;
/****************************************************************
* The output should be:
*
* Creating sample.dat.
* Successfully read from the stream the following:
* Sample Program.
*/
}
C Program to input a Number from user and swap first and last digit of the given number. First input a number from user and find last digit. Total number of digit - 1. Find first digit.
Logic to find all Armstrong number between 1 to n. Input Upper Limit to print Armstrong number from user. Store it in some variable say end. Run a loop from 1 to end, increment
C program to read elements in a matrix and find determinant of the given matrix. Find determinant of a 2x2 matrix and 3x3 matrix. Logic to find determinant of a matrix in C.