UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

9.2.4.  Opening a file with a file descriptor

In order to open a file using its file descriptor (fd), you must use the fdopen function. This function behaves as fopen, but instead of opening a file using its name or path, it uses the file descriptor:

#include <stdio.h>
int fdopen(int fildes, const char *mode);

A way to get a file descriptor is with mkstemp:

#include <stdio.h>
int mkstemp(char *template);

This function generates a unique temporary file name from template string. The last six characters of template must be "XXXXXX" and these are replaced with a string that makes the filename unique. The file is then created for reading or writing. Since the string will be modified, template must not be a string constant, but should be declared as a character array. The mkstemp function returns the file descriptor of the temporary file or -1 on error.