Jump to content

Check out our Community Blogs


Member Since 01 Sep 2011
Offline Last Active Sep 02 2011 03:22 PM

Topics I've Started

fstream overloading

01 September 2011 - 10:35 PM

Hi everyone! I'm new here and I have a problem to solve, hope someone good with C++ can help me.

These days I've studied operator overloading and after that I tried to overload >> operator with ifstream for files.

I'm wondering why this program doesn't show me the data I'm trying to get from the file dati.it USING THE OP. OVERLOADING. Without the function ifstream& operator>>(ifstream& c, string& s) it works fine, but I want to do the job with the overloading :( I know it's a simple stupid example but it's for learning... what's wrong with it? (I'm getting no errors, just not doing what I want)

#include <iostream>

    #include <fstream>

    using namespace std;


    ifstream& operator>>(ifstream& c, string& s) {

        c >> s;

        return c;



    int main()


        const int dim_max = 1000;

        char name[dim_max];

        char surname[dim_max];

        char email[dim_max];

        string lista;


        // 1.input

        cin.getline(name, dim_max, '\n');

        cin.getline(surname, dim_max, '\n');

        cin.getline(email, dim_max, '\n');


        // writing to file

        ofstream Fileobj;


        Fileobj << name << endl << "#" << endl << surname << endl << "#" << endl << email << endl << "#" << endl;



        // 2.output

        ifstream Fileobjx("dati.txt");


        while (!Fileobjx.eof()) {

            Fileobjx >> lista;

            cout << lista;



        cout << "Ok" << endl;

        return 0;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download