You can use sprintf
or snprintf
to print to a char *
buffer, and then use write
. To get a file descriptor from a FILE *
variable, you can use fileno
. There is no portable way to go from a file descriptor to a FILE *
, though: you can portably to use fdopen
to associate a FILE *
with a valid file descriptor.
In addition, the latest POSIX standard specifies dprintf
, but the GNU libc dprintf
man page has this to say:
These functions are GNU extensions, not in C or POSIX. Clearly, the
names were badly chosen. Many systems (like MacOS) have incompatible
functions called dprintf()
, usually some debugging version of printf()
,
perhaps with a prototype like
void dprintf (int level, const char *format, ...);
where the first parameter is a debugging level (and output is to
stderr
). Moreover, dprintf()
(or DPRINTF
) is also a popular macro name
for a debugging printf
. So, probably, it is better to avoid this function in programs intended to be portable.
Of course, the libc manual page is not updated with the latest standard in mind, but you still have to be careful with using dprintf
, since you might get something you don't want. :-)