I'm playing around with mkstemp()
, which provides a file descriptor, but I want to generate formatted output via fprintf()
. Is there an easy way to transform the file descriptor provided by mkstemp()
into a FILE *
structure that is suitable for use with fprintf()
?
The file descriptor is just an integer that you get from the open() system call. Example of file descriptor: int fd = open(filePath, mode); File pointer is a pointer returned by fopen() library function.
You pass "naked" file descriptors to actual Unix calls, such as read() , write() and so on. A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the file descriptor, and adds buffering and other features to make I/O easier.
FILE *fp; To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.
When a process makes a successful request to open a file, the kernel returns a file descriptor which points to an entry in the kernel's global file table. The file table entry contains information such as the inode of the file, byte offset, and the access restrictions for that data stream (read-only, write-only, etc.).
Use fdopen()
:
FILE* fp = fdopen(fd, "w");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With