Last active
February 2, 2017 10:55
-
-
Save bzdgn/d0f1db11bab1fa2399ad68b7d69f2a44 to your computer and use it in GitHub Desktop.
A Simple CPP Hello World For ofstream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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