c_fixed_length_record

Table Of Contents

Introduction, c_fixed_length_record

This object provides a simple way to work with fixed length data, such as fixed length flat files. You initialize the object with a set of 'fields' for which you define the start position, and end position for the field. You can then pass a string into the object and it will populate the fields based on the positions specified.

Example Code:
void foo( void ) 
{
  // it would probably be more useful to do this from a file...
  char* name1 = "     James  Goseling       Sun Microsystems20112456";
  char* name2 = "     Larry      Wall  O'Reilly & Associates22153270";
  char* name3 = "    BjarneStroustrup                   AT&T21653982"
  c_fixed_length_record person, runs_on_anything, 
                        runs_the_web, can_build_anything;

  person.add_field("first_name",       "0",  "9");
  person.add_field("last_name",       "10", "19");
  person.add_field("employer",        "20", "42");
  person.add_field("unknown number",  "43", "50");
  runs_on_anything   = person;
  runs_the_web       = person;
  can_build_anything = person;

  runs_on_anything.parse(name1);
  dump_personal_info(runs_on_anything);

  runs_the_web.parse(name2);
  dump_personal_info(runs_the_web);

  can_build_anything.parse(name3);
  dump_personal_info(can_build_anything);
}

void dump_personal_info( const c_fixed_length_record& p )
{
  c_string fname, lname, empl;

  p.get_value( "first_name", fname );
  p.get_value( "last_name",  lname );
  p.get_value( "employer",   empl );

  cout << fname << " " << lname << "'s employer is: " << empl << endl;
}

Up