'How to close IStream?
How can i close IStream in c++?
Solution 1:[1]
If you mean COM IStream, just call IUnknown::Release().
Solution 2:[2]
You can close file iostreams with ifstream::close. closing a istream doesn't really make sense - should it close the shell the program is started from?
You can flush an iostream to ensure the output is written
Solution 3:[3]
There are two standard subclasses of std::istream, which are closeable. If you want to close them in a context, where you only see the base class, you first need to cast to the instantiated subclass.
std::istream& is ....
// close is
std::ifstream* ifs = dynamic_cast<std::ifstream*>(&is);
if (ifs!=0)
{
ifs->close();
}
else
{
std::fstream* fs = dynamic_cast<std::fstream*>(&is);
if (fs!=0)
{
fs->close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nemanja Trifunovic |
| Solution 2 | Martin Beckett |
| Solution 3 | Sam Ginrich |
