Is it possible in C/C++ to create my own custom stream of type FILE (stdio.h) that can be used with fputs() for example ?
No it isn't. FILE is what is known as an opaque type - it doesn't have anything you can customise.
It is possible. But you should link your application with your version of fputs(), etc instead of those coming with the standard libraries. Most compilers link to the standard IO libraries by default. You have to figure out how to get around that.
There is no standard way with the FILE interface. Note that C++ code usually should use IOStream for IO, with the IOStream interface, writing a custom streambuf is the solution to your problem.
If you can drop standard conformance, look at the documentation of your implementation.
POSIX has a popen() function which create a FILE* reading from the output or writing to the input of a process;
POSIX has a fdopen() which is able to create a FILE* from a file descriptor;
At least for the GNU libc used by Linux, there is a possibility of defining custom C stream (see the "Programming your awn custom streams" section in the info documentation).
If you are compiling with the GNU C library you could use fmemopen(3):
fmemopen, open_memstream, open_wmemstream - open memory as stream
If your "custom stream" isn't something you can represent with a file descriptor or file handle, then you're out of luck. The FILE
type is implementation-defined, so there's no standard way to associate other things with one.
If you can get a C file descriptor for whatever it is you're trying to write to, then you can call fdopen
on it to turn it into a FILE*
. It's not standard C or C++, but it's provided by Posix. On Windows, it's spelled _fdopen
.
If you're using Windows and you have a HANDLE
, then you can use _open_osfhandle
to associate a file descriptor with it, and then use _fdopen
from there.
Are you really tied to fputs
? If not, then replace it with use of a C++ IOStream. Then you can provide your own descendant of std::basic_streambuf
, wrap it in a std::ostream
, and use standard C++ I/O on it.