Click to See Complete Forum and Search --> : Operator Overloading


Lil'Hasher
February 13th, 2003, 09:19 AM
Hello,

I'm trying to read a record in from a file. I have a record class with char* for two fields, name and last name say. However when overloading the >> operator so that i can read from a file dircetly into a record objec, i run into this problem :

I have two files of two slightly different formats, I would like to be able to read into a class record object from both those formats.
So is there a way to overload the << operator twice and somehow distinguish which is for which file?

ie. File 1 : Last Name [6 characters of blanks] First Name
File 2 : Last Name First Name (with no spaces)


thanks

KevinHall
February 13th, 2003, 11:55 AM
You have two options:

(1) Define two different file classes:


FileClass1& operator>> (FileClass1& f, MyRecord& r);
FileClass2& operator>> (FileClass2& f, MyRecord& r);


(2) Differentiate between file types within the overloaded operator:


std::istream& operator>> (std::istream& s, MyRecord& r)
{
std::string str;

getline(s, str);

//determine file type from string and scan data from str
}


You have enough information to figure this out -- namely how many spaces are in the string. I'm confident you can figure things out from here!