• Advertisement
Sign in to follow this  

Iostream query

This topic is 4585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement