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

STL + IOStream

Started by MindCode May 22, 2003 at 10:32 PM 6 replies 600+ views
Original Post
MindCode
MindCode
is there some interface between stl and iostream that allows the streaming of a whole container to a file? Something like this vector lotsOfData; ofstream outFileStream; outFileStream.write(lotsOfData.begin(), lotsOfData.end()); I know stl and iostream aren''t the same library but I figured there might be some interface since both are std. Right now I''m running a loop on the container and writing each element individually.
That's just my understanding; I could be wrong.
MindCode
MindCode
What if the container holds structures or classes? And what the heck does the string do?

[edited by - MindCode on May 22, 2003 12:15:03 AM]
That's just my understanding; I could be wrong.
Beer Hunter
Beer Hunter
...it effectively calls outFileStream << element << " "; for every element in the sequence. I gave you links...
daerid
daerid
Make sure you have operator << defined for your class so that it supports insertion into a stream
daerid@gmail.com
C-Junkie
C-Junkie
or define you own

ostream& operator<<(ostream &out, vector it)
{
FOR EACH THINGY IN it:
out << JUNK IN THINGY;
}

and fp << your_vector.
Lektrix
Lektrix
quote:
Original post by C-Junkie
or define you own


Well that's what daerid means when he says, "make sure you have operator << defined for your class so that it supports insertion into a stream".

quote:
Original post by C-Junkie
ostream& operator<<(ostream &out, vector it)
{
FOR EACH THINGY IN it:
out << JUNK IN THINGY;
}


That should be:

std::ostream& operator<<(std::ostream& os, const vector& obj)
{
    // os << obj.Values(); etc, whatever

    return os;   // return a reference to allow linking
}


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 24, 2003 3:57:42 AM]
mfawcett
mfawcett
std::ifstream fin("file.txt");
std::stringstream buf;

if (fin.is_open())
{
buf << fin.rdbuf();
}
fin.close();

--Michael Fawcett

Topic Locked

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

Sign in to reply to this topic.