Method 4, fields with keyword tags.
Ans>
Assume that
- the format of the keyword tags is “keyword=value,”
- the maximum length of
keyword
is less than 32 bytes,
- the maximum length of
value
is less than 256 bytes, and
- the delimiter is ‘|’.
istream & operator >> ( istream & stream, Person & p ) {
char keyword[32];
for ( int i = 0; i < 6; i++ ) {
stream.getline( keyword, 32, '=' );
if ( strncmp( keyword, "LastName", strlen( "LastName" ) ) == 0 )
stream.getline( p.LastName, 256, '|' );
else if ( strncmp( keyword, "FirstName", strlen( "FirstName" ) ) == 0 )
stream.getline( p.FirstName, 256, '|' );
else if ( strncmp( keyword, "Address", strlen( "Address" ) ) == 0 )
stream.getline( p.Address, 256, '|' );
else if ( strncmp( keyword, "City", strlen( "City" ) ) == 0 )
stream.getline( p.City, 256, '|' );
else if ( strncmp( keyword, "State", strlen( "State" ) ) == 0 )
stream.getline( p.State, 256, '|' );
else if ( strncmp( keyword, "ZipCode", strlen( "ZipCode" ) ) == 0 )
stream.getline( p.ZipCode, 256, '|' );
else if ( strlen( keyword ) == 0 ) return( stream );
}
return( stream );
}