472,345 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

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

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
0 1620

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the...
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with...
by: CSN | last post by:
Is it possible to iterate over an array in plpgsql? Something like: function insert_stuff (rel_ids int) .... foreach rel_ids as id insert...
by: Luke Vogel | last post by:
Hi all, Probably a really basic question, but I cant find an answer ... I have an xml file of books something like: <product>...
by: VB Programmer | last post by:
I would appreciate your assistance on this ASP.NET 2.0 site.... This is the wierd problem: While accessing the built in .NET functions for...
by: bonk | last post by:
Hello, I am currently trying to wrap my head around what actually happens when I compile a normal (native) c++ class with the /CLR Flag in Visual...
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and...
by: boris.smirnov | last post by:
Hi all, I have a python script that works without any problem on Windows but with error: QPaintDevice: Must construct a QApplication before a...
by: moondaddy | last post by:
I'm running a wpf windows app and want to add the appSettings element to the config file. when I do, the app wont start up and I get the following...
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.