You are on page 1of 2

#include <iostream>

#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stud
{
int matricol;
public:
string nume;
Stud(int m = 0, string n = "Noname") : matricol(m), nume(n) {}

friend ostream& operator<<(ostream& ost, Stud& s)


{
ost << s.matricol << "\t" << s.nume; return ost;
}

friend istream& operator>>(istream& ist, Stud& s)


{
cout << "\nMatricol: "; ist >> s.matricol;
cout << "\nNume: "; ist.ignore(50, '\n'); getline(ist, s.nume);
return ist;
}

/*friend ofstream& operator<<(ofstream& ost, Stud& s)


{
ost << s.matricol << endl; ost << s.nume<<endl;
return ost;
}

friend ifstream& operator>>(ifstream& ist, Stud& s)


{
ist >> s.matricol;
ist.ignore(50, '\n'); getline(ist, s.nume);
return ist;
}*/

friend ofstream& operator<<(ofstream& ost, Stud& s)


{
ost.write((char*)&s.matricol, sizeof(int));
int sz = s.nume.size(); ost.write((char*)&sz, sizeof(int));
ost.write((char*)&s.nume[0], sz);
return ost;
}

friend ifstream& operator>>(ifstream& ist, Stud& s)


{
ist.read((char*) &s.matricol, sizeof(int));
int sz=0; ist.read((char*)&sz, sizeof(int));
s.nume.resize(sz + 1); ist.read((char*)&s.nume[0], sz);
return ist;
}

};
ostream& money_format(ostream& ost)
{
ost << setiosflags(ios::fixed);
ost << setprecision(5) << setfill('$') << setw(15);
ost << setiosflags(ios::showpoint);
return ost;
}

int main()
{
Stud s1, s2(102, "Adomnitei Ana-Maria"), s3, s4;
cout << "\n Initial: " << "\n\t" << s1 << "\n\t" << s2;
cin >> s1;
cout << "\n Citit: " << "\n\t" << s1 << "\n\t" << s2;
cout << "\n sizeof(nume) : " << sizeof(s1.nume) << "\t size : " <<
s1.nume.size();

ofstream fisOut("stud.dat");
if (!fisOut) { cerr << "\nEroare fisier scriere"; exit(2); }
fisOut << s1 << s2; fisOut.close();

ifstream fisIn("stud.dat");
if (!fisIn) { cerr << "\nEroare fisiers citire"; exit(1); }
fisIn >> s3 >> s4; fisIn.close();
cout << "\n Citit disc: " << "\n\t" << s3 << "\n\t" << s4;

double a = 12345.6789;
cout << endl << money_format << a << setprecision(2); //$$$$12345.67890
cout << "\n" << a; // 12345.68

cin.get();
}

You might also like