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()?
+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 viafclose(3)
,fildes
is closed also.
Gregory Pakosz
2009-12-21 17:32:15
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
2009-12-21 17:32:35
Depends on what you mean by "standard". POSIX is a standard.
Richard Pennington
2009-12-21 17:33:40
The question was about C.
anon
2009-12-21 17:34:37
though, The fdopen() function conforms to IEEE Std 1003.1-1988 (``POSIX.1'').
Gregory Pakosz
2009-12-21 17:34:39
The question was about file descriptors. ;-)
Richard Pennington
2009-12-21 17:35:33
@Neil > you might be right, you didn't help the OP with your answer
Gregory Pakosz
2009-12-21 17:35:56
@Gregory I helped him assuming that he wants to write portable code. And downvoting technically correct answers is not good practice.
anon
2009-12-21 17:38:26
@Richard The question is tagged as C, not Posix. Posix is a platform, not a language, standard.
anon
2009-12-21 17:39:25
@Neil > I didn't vote down, indeed there is nothing wrong in what you said.
Gregory Pakosz
2009-12-21 17:40:03
@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
2009-12-21 17:42:09
@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
2009-12-21 17:44:11
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
2009-12-21 17:53:33
@Neil > ok, have a good evening
Gregory Pakosz
2009-12-21 17:56:30
@Neil > it looks like you have fans though, as several of my answers are being downvoted right now :)
Gregory Pakosz
2009-12-21 17:57:18
I'm backing away slowly... Now turning and running away.
Richard Pennington
2009-12-21 17:59:05
@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
2009-12-21 17:59:18
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
2009-12-21 17:59:29
@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
2009-12-21 18:08:20