|
-
February 13th, 2003, 10:19 AM
#1
Operator Overloading
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
-
February 13th, 2003, 12:55 PM
#2
You have two options:
(1) Define two different file classes:
Code:
FileClass1& operator>> (FileClass1& f, MyRecord& r);
FileClass2& operator>> (FileClass2& f, MyRecord& r);
(2) Differentiate between file types within the overloaded operator:
Code:
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|