Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a FILE pointer from a file descriptor?

Tags:

c

posix

mkstemp

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()?

like image 779
BD at Rivenhill Avatar asked Dec 21 '09 17:12

BD at Rivenhill


People also ask

Is a file pointer a file descriptor?

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.

Is file pointer same as file descriptor?

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.

How do I access file pointer?

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.

What does a file descriptor point to?

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.).


Video Answer


1 Answers

Use fdopen():

FILE* fp = fdopen(fd, "w"); 
like image 125
Richard Pennington Avatar answered Oct 08 '22 08:10

Richard Pennington