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 ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your “custom stream” isn’t something you can represent with a file descriptor or file handle, then you’re out of luck. The
FILEtype 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
fdopenon it to turn it into aFILE*. 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_osfhandleto associate a file descriptor with it, and then use_fdopenfrom 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 ofstd::basic_streambuf, wrap it in astd::ostream, and use standard C++ I/O on it.