#include #include #include using namespace std; class clovek { int vek; string jmeno; public: clovek(int vek = 0, string jmeno = "") { this->vek = vek; this->jmeno = jmeno; } void ulozDoSouboru(string cesta) { ofstream f(cesta); if(f.is_open() == true) { f << vek << endl; f << jmeno << endl; f.close(); } } void nactiZeSouboru(string cesta) { ifstream f(cesta); if(f.is_open() == true) { string buffer = ""; getline(f, buffer); // prevod retezece na cislo // .c_str() prevede string na const char * vek = atoi(buffer.c_str()); getline(f, jmeno); f.close(); } } void vypis() { cout << jmeno << " " << vek << endl; } void vypisDoStreamu(ofstream& o) { o << vek << endl; o << jmeno << endl; } void pridatDoSouboru(string cesta) { ofstream f(cesta, ios::app); if(f.is_open() == true) { f << vek << endl; f << jmeno << endl; f.close(); } } void nactiZeStreamu(ifstream& f) { string buffer = ""; getline(f, buffer); vek = atoi(buffer.c_str()); getline(f, jmeno); } }; int main(int argc, char** argv) { clovek c(183, "Petr Novak"); c.ulozDoSouboru("pokus.txt"); c.vypis(); clovek d; d.nactiZeSouboru("pokus.txt"); d.vypis(); c.pridatDoSouboru("pokus.txt"); /*// ulozeni pole instanci tridy clovrek do souboru int pocet = 3; clovek lidi[pocet]; lidi[0] = clovek(183, "Petr Novak"); lidi[1] = clovek(184, "Petr Novak1"); lidi[2] = clovek(185, "Petr Novak2"); ofstream f("pokus1.txt"); if(f.is_open() == true) { f << pocet << endl; for(int i = 0; i < pocet; i++) lidi[i].vypisDoStreamu(f); f.close(); } */ // nacteni pole instanci do souboru clovek* lidi; int pocet = 0; ifstream f("pokus1.txt"); if(f.is_open() == true) { string buffer = ""; getline(f, buffer); pocet = atoi(buffer.c_str()); lidi = new clovek[pocet]; for(int i = 0; i < pocet; i++) lidi[i].nactiZeStreamu(f); f.close(); } for (int i =0; i> a; i >> b; i.close(); cout << a << " " << b << endl; return 0; }