Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Last active February 2, 2017 10:55
Show Gist options
  • Save bzdgn/d0f1db11bab1fa2399ad68b7d69f2a44 to your computer and use it in GitHub Desktop.
Save bzdgn/d0f1db11bab1fa2399ad68b7d69f2a44 to your computer and use it in GitHub Desktop.
A Simple CPP Hello World For ofstream
#include <string>
#include <vector>
#include <fstream>
using namespace std;
struct Journal
{
string title;
vector<std::string> entries;
/*
* Note: Don't use curly braces for constructor initializer
* http://stackoverflow.com/questions/27741521/error-c2797-list-initialization-inside-member-initializer-list
*/
explicit Journal(const string& title) : title( title )
{ }
void add(const string& entry)
{
entries.push_back(entry);
}
};
struct PersistenceManager
{
static void save(const Journal& j, const string& filename)
{
ofstream ofs(filename);
for (auto& s : j.entries)
ofs << s << endl;
}
};
int main()
{
Journal j = Journal("Levent's Journal");
j.add("Today is nice");
j.add("Tomorrow is great!!!");
PersistenceManager p = PersistenceManager();
p.save(j, j.title);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment