Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

fstream overloading

stream

  • Please log in to reply
4 replies to this topic

#1 Megaz

Megaz

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 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.open("dati.txt");

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

        Fileobj.close();

     

        // 2.output

        ifstream Fileobjx("dati.txt");

     

        while (!Fileobjx.eof()) {

            Fileobjx >> lista;

            cout << lista;

        }

     

        cout << "Ok" << endl;

        return 0;

    }

  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 September 2011 - 04:38 AM

I'm surprised it even compiles. Think about trying to execute it by hand.

Fileobjx >> lista is interpreted as operator>>(Fileobjx, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
The first line of that is c >> s, which is interpreted as operator>>(c, s) and calls your operator overload.
.
.
.

See the problem?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Megaz

Megaz

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 02 September 2011 - 07:29 AM

Thanks for helping... but how it must be so? :crying:
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 September 2011 - 08:54 AM

Because you haven't told it to use the default fstream.operator>>, so it uses the one you provided.

As a general rule, overloading an operator with parameters that have already been defined is a bad idea ™. Now, if you create a class that has a single public string property, and define operator>> on the class in terms of operator>> on the class's string, that will probably work just fine.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 Megaz

Megaz

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 02 September 2011 - 03:21 PM

Because you haven't told it to use the default fstream.operator>>, so it uses the one you provided.

As a general rule, overloading an operator with parameters that have already been defined is a bad idea ™. Now, if you create a class that has a single public string property, and define operator>> on the class in terms of operator>> on the class's string, that will probably work just fine.


This helped me to understand more on overloading as well!

Thanks for your help! <3
  • 0





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