View Single Post
  #4 (permalink)  
Old May 16th, 2016, 04:08 AM
Marc Gregoire Marc Gregoire is offline
Wrox Author
Points: 220, Level: 4
Points: 220, Level: 4 Points: 220, Level: 4 Points: 220, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You'll have to format the data in such a way that you can read it back.
If you look at the createMuffin() method, you see that it is able to parse/read muffins formatted as follows:
Muffin_Name Size Chips
So, name followed by a space, followed by the size, a space and true or false for the chocolade chips.
If you want to use createMuffin to read data from a file, then you should modify output() to write it in that format. So, for example:
Code:
void Muffin::output()
{
	ofstream outFile("Muffin.txt", ios::out | ios::app);
        if(!outFile.is_open()) {
             cerr << "File could not open!";
             return;
        }
	outFile << getDescription() << " " << getSize() << " " << boolalpha << getHasChocolateChips() << '\n';
}
Some notes:
* No need for the clear() or close(). The file is automatically closed.
* booalpha is used to write true/false instead of 1/0
* no need for the .c_str() when writing a std::string to a C++ stream.
* This obviously only works if there are no spaces in the name of the muffin. If you want to support spaces in the name, then you can take a look at the quoted manipulator. (see https://msdn.microsoft.com/en-us/library/dn986842.aspx)