TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: THOMAS MAEDER
from: DARIN MCBRIDE
date: 1997-12-08 18:24:00
subject: Parsing a file

 TM> And what you want to write is:
 TM> Person aPerson;
 TM> iasciistream inFile("filename");
 TM> if (inFile >> aPerson)
 TM>     // reading was a success, use aPerson
 TM> else
 TM>     // oops
 TM> iasciistream is a class you have to derive from istream. Overload  the
 TM> operator>>  for string, int, double and maybe other types according to
 TM> your file structure (input operations have to eat the  commas,  string
No, you don't need to create your own class.  Just have a friend to Person 
who is the overloaded operator>>.
 TM> iostream &operator>>(iostream &ias, Person &aPerson)
 TM> {
 TM>     return  ias >> aPerson.Id 
 TM>                 >> aPerson.FirstName 
 TM>                 >> aPerson.LastName
 TM>                 >> aPerson.aString
 TM>                 >> aPerson.anInt
 TM>                 >> aPerson.anotherInt
 TM>                 >> aPerson.aDouble;
 TM> }
There ya go - that's all that's needed.
If it comes in from a string, a file, a modem, whatever, this will read in 
the strings.  However, his example was much more complicated than this, 
requiring some parsing of quotes and commas.
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.