By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

How should I construct my objects when reading ctor args from a text file?

P: n/a
Hello, I have a text file that contains a number of entries describing a
recipe. Each entry consists of a number of strings. Here's an example file
with only one entry (recipe):
Name=Maple Quill
Primary components=Refined Maple
Build components=1 Stroma Oil
Fuel=1 abrasive sandpaper

I've made a function that reads this text file and for each valid and
complete entry is supposed to create an object of type Recipe (a simple
class I wrote) and add it to a std::vector<Recipe>. The constructor of the
class Recipe is:

Recipe(const std::string& name,
const std::string& process,
unsigned short level,
const std::string& technique,
const std::string& knowledge,
const std::string& device,
/* There exists an "or" relationship between each primary
component. */
const std::vector<std::string>& primary_components,
/* There exists an "and" relationship between build components. */
const std::vector<std::string>& build_components,
const std::string& fuel)
for(std::vector<std::string>::size_type i = 0; i <
primary_components.size(); ++i)

for(std::vector<std::string>::size_type i = 0; i <
build_components.size(); ++i)

The function reading the text file (along with a simple helper function) is:

static void
read_entries(ifstream& file, vector<Recipe>& recipes)
const unsigned short NAME = 0;
const unsigned short PROCESS = 1;
const unsigned short LEVEL = 2;
const unsigned short TECHNIQUE = 3;
const unsigned short KNOWLEDGE = 4;
const unsigned short DEVICE = 5;
const unsigned short PRIMARY_COMPONENTS = 6;
const unsigned short BUILD_COMPONENTS = 7;
const unsigned short FUEL = 8;

vector<string> mm(9);

mm[NAME] = "Name";
mm[PROCESS] = "Process";
mm[LEVEL] = "Level";
mm[TECHNIQUE] = "Technique";
mm[KNOWLEDGE] = "Knowledge";
mm[DEVICE] = "Device";
mm[PRIMARY_COMPONENTS] = "Primary components";
mm[BUILD_COMPONENTS] = "Build components";
mm[FUEL] = "Fuel";

string current_line;
unsigned short pos = 0;
unsigned short num_recipes = 0;

while(getline(file, current_line))
if(!current_line.length() || current_line[0] == '#') /* # denotes
comment */

if(!starts_with(current_line, mm[pos]))
cerr << "Attempted to read option " << mm[pos]
<< ", but found only " << current_line << endl;


/* +1 to account for = */
string value = current_line.substr(mm[pos].length() + 1,
cout << "read value " << value << " for option " << mm[pos] << endl;

if(++pos == mm.size())
cout << "Recipe data read for recipe " << ++num_recipes << endl;
pos = 0;

static bool
starts_with(const string& s, const string& starts_with)
if(s.length() < starts_with.length())
return false;

string str = s.substr(0, starts_with.length());

if(str == starts_with)
return true;

return false;

As you can see the value ends up in the variable value (of type
The ouput of the program when I run with a text file containing the one
entry above is:
read value Maple Quill for option Name
read value Interim for option Process
read value 10 for option Level
read value Fletching for option Technique
read value Woodworking for option Knowledge
read value Sawhorse for option Device
read value Refined Maple for option Primary components
read value 1 Stroma Oil for option Build components
read value 1 abrasive sandpaper for option Fuel
Recipe data read for recipe 1

Oops, this turned out to be a long message...anyway, my question: How should
I go about creating my Recipe object in this function? I am reading each
argument to the Recipe constructor one at a time from the file and it ends
up in a std::string, not sure how I should go from that to actually
constructing an object of type Recipe? Please help me with this homework.

Thanks for reading and replying

/ Eric
Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.