tags:

views:

305

answers:

3

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

+11  A: 

Use fdopen():

FILE* fp = fdopen(fd, "w");
Richard Pennington
+7  A: 

FILE* f = fdopen(d, "w");

man fdopen output:

SYNOPSIS

#include <stdio.h>

FILE *
fdopen(int fildes, const char *mode);

The fdopen() function associates a stream with the existing file descriptor, fildes. The mode of the stream must be compatible with the mode of the file descriptor. When the stream is closed via fclose(3), fildes is closed also.

Gregory Pakosz
A: 

There is no standard way of doing this (or the reverse) as the C Standard has nothing to say about file descriptors. Your specific platform may or may not provide such a mechanism.

anon
Depends on what you mean by "standard". POSIX is a standard.
Richard Pennington
The question was about C.
anon
though, The fdopen() function conforms to IEEE Std 1003.1-1988 (``POSIX.1'').
Gregory Pakosz
The question was about file descriptors. ;-)
Richard Pennington
@Neil > you might be right, you didn't help the OP with your answer
Gregory Pakosz
@Gregory I helped him assuming that he wants to write portable code. And downvoting technically correct answers is not good practice.
anon
@Richard The question is tagged as C, not Posix. Posix is a platform, not a language, standard.
anon
@Neil > I didn't vote down, indeed there is nothing wrong in what you said.
Gregory Pakosz
@Neil > the question being tagged as C doesn't mean "tell me the holly truth about the C standard" but rather "I'm coding in C, I fail at doing this, please tell me whether it is possible and how?"
Gregory Pakosz
@Gregory You obviously don't understand the purpose of SO. That being the case, this will be my last reply to you or to any of your posts here. Not a great loss.
anon
There is a `posix` tag here on SO, with 193 questions. Having said that, I think Neil's answer is correct. More people should be using `posix` tag. And it's a good idea to remind people what exactly "C" is. While we're at it, we should make people realize that there's nothing like "C/C++", and that these are two different languages.
Alok
@Neil > ok, have a good evening
Gregory Pakosz
@Neil > it looks like you have fans though, as several of my answers are being downvoted right now :)
Gregory Pakosz
I'm backing away slowly... Now turning and running away.
Richard Pennington
@Gregory: For the record, it wasn't me. There are some weird downvoting issues here on SO. My posts http://stackoverflow.com/questions/1898371/problems-writing-the-memset-function/1898515#1898515 and http://stackoverflow.com/questions/1938491/memory-corruption/1938530#1938530 were downvoted, and I don't know why!
Alok
I agree that there's no standard way of doing this, but the OP's mention of `mkstemp` implies that non-standard solutions are acceptable in this particular instance. In that case, however, he should have added the `posix` tag (which I see has since been added).
Emerick Rogul
@Neil, and the purpose of the Net and the meaning of life. You would make more sense if you've said that you are of different opinion. I also doubt SO is all about hairsplitting over standards. And it's not very practical to rely on tagging skills of someone with an SO-experience corresponding to the reputation of 23.
Michael Krelin - hacker