Skip to main content
GameDev.net gamedev.net
🔒 Locked

Iostream query

Started by ugoel123 Jul 31, 2005 at 11:31 PM 3 replies 750+ views
Original Post
ugoel123
ugoel123
Hi, I have a file pointer(FILE *) of unicode file. Can I somehow put this file pointer to the iostream so that iostream will use it as its internally generated file space.
snk_kid
snk_kid
Short answer no, very very long answer very slight possibility but it would be non-portable so it would be useless anyways.

Why not just use the C++ iostream & locales directly?

If you really must do something like this one possibility would be derive your own from basic_streambuf & basic_io/i/ostream.
MrEvil
MrEvil
[headshake] It's not part of the standard.

You could try something like this, but it is non-standard and will only work in Borland's (and possibly Microsoft's) compilers.
#include<cstdio>#include<iostream>int main(){    FILE * fp = reinterpret_cast<FILE*>(0xdeadbeef); //XXX get file handle here    std::wfilebuf buffer(fp);    std::wifstream stream(buffer);       stream << L"Right. This marble is as slippery as this marble." << std::endl;    }

See Rogue Wave's documentation (Rogue Wave made the standard library for Borland's compiler).

PS. snk_kid is right, making a class that derives from streambuf would be easy to do, since all the operations are almost done for you. No need even for an actual buffer, since that's already done for you. http://cplusplus.com/ref/iostream/filebuf
PPS. In fact, you might be able to just copy the code from your existing standard library, keeping in mind the licences on these files, and just add a few functions (overloads for constructor + open() functions), then call it exfilebuf or something.
ugoel123
ugoel123
Hi all,
I tried to derive my class with iostream and overriden the open function. I can't do lot of things since base class fstream is using some private function to set some private variable. I can't do that in my class. So I am pretty much stuck there since even without those private members intialization i can open the file but can't read properly.

Any help on this will be highly appreciated.
MrEvil
MrEvil
You'll have to derive from streambuf to make it work. It's easily hackable in a day if you know what you're doing. Also, don't forget to template everything so that you can use wide and narrow strings.

It's probably easier to convert to using iostreams...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.