Hi,

I've just joined this website for help with some C++ problems. I am new to C++ and face the task of reading from a text file and outputing the result in another text file using the istream& operator >>(istream& stream, storage &s)
it looks like something underneath:
istream& operator >>(istream& stream, storage &s)
{
...
}

The text file consist of integers and it needs to be read in the other it is placed in the original file.

Any help will be welcome.

Recommended Answers

All 3 Replies

You have posted a pretty typical prototype for overloading the >> operator for class storage, whatever that is. This allows you to read a storage object from a file using the >> operator rather than writing code to read the member variables one at a time, using any istream object, like a ifstream object or an fstream object in ios::in mode. You can do something similar with the << operator to write a storage object to a file. Of course you also need to implement the prototype, but without knowing what the storage class is, who know what that will entail.

Hi,

The storage class is a vector. I think I've implemented the ifstream operator here's a fragment of the code.

ostream& operator << (ostream &stream, storage &s)
{
    for(int i =0; i < s.vectorData.size() ; i++)
    {
        stream<<s.vectorData[i]<<endl;

    }
    return stream;
}

vectorData is defined as vector <int> vectorData; Which is defined in the header file.

Regards

Without seeing the details of the storage class that could function as an overloaded << operator, which is actually a member of the ostream class, not the istream or ifstream class. It could be used to write the vector to the screen with a single call to the << operator or it could be used to write the vector data to file if an ofstream or fstream in ios::out mode were sent as the first parameter. Usually the second parameter for the overloaded << operator would be sent using keyword const as the operator shouldn't change any of the values being displayed/written.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.