Laboratorium 2

 

4. Pliki: zapisz do pliku imie, nazwisko , nr indeksu.

#include <fstream>

using namespace std;

void main(){

char imie[20]=”Jan”;

 

      ofstream of(”out.txt”);

      if(!of){cerr<<”Bład”<<endl;return;}

      of << imie << “ “<<nazwisko <<” “ <<nr_albumu<<endl;

      // cout << imie << “ “<<nazwisko <<” “ <<nr_albumu<<endl;

      // of.close();

}

 

 

ifstream ifs(„out.txt”);

if(!ifs){

 cerr<<”nie moge otworzyc pliku”<<endl;

exit(0)

}

ifs>>imie>>nazwisko>>nr_albumu;

// wypisac dane

 

Hierarchia klas plików

istream (cin)

     ifstream

     istrstrem

ostream (cout)

     ofstream

     ostrstream

 

 

 

5 Klasa Student

 

class Student

{

public:

   Student();

   Student(const char*im,const char*naz,const char*nra);

 

   void read(istream&is);

   void write(ostream&os) const;

 

   char imie[64];

   char nazwisko[64];

   char nr_albumu[16];

};

 

 

void main(){

Student s1;

s1.read(cin);

s1.write(cout);

 

Student s2(“Jan”, “Nowak”, “1222”);

s2.write(cout);

 

ofstream of(„out.txt”);

s1.write(of);

s2.write(of);

}

 

Student::Student()

{

}

 

void Student::write(ostream&os) const

{

}

 

 

5.b Dodaj klasę Grupa

class Grupa

{

public:

 

#define SIZE 100

Student tab[SIZE];

     int liczba;

 

     Grupa();

};

 

 

5.c Dodać dwie funkcje do klasy Grupa

bool add(Student&s)

bool add(const char*imie, const char*nazw, const char*nr)

 

5.d Zadanie domowe:  

Proszę dodać funkcję, która zapisuje zawartość grupy. Jak mogłaby wyglądać funkcja umożliwiająca odczyt?

void write(ostream&os)const;

 

void main()

{

Grupa g;

g.add(“Jan”,”Kowalski”,”123”);

g.add(“Anna”,”Kowalska”,”124”);

g.write(cout);

}