I got few questions about this code.
dat - is that a class in iostream? I tried looking it up on google, with no results. Weird.
Is "fstream" analogous to C's FILE structure?
I'd also like to know why my dat.write() wont work.
When i enter some data, and call dat.write to write the structure, the data doesnt appear in my file.
I also couldnt find dat.eof() with google. Is there a site that tells about these functions?
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct tstudent {
int mat_br;
char prez_ime[35];
int g_stu;
};
tstudent student;
fstream dat;
char naziv_datoteke[40];
void kreiranje_datoteke();
void unos_podataka_u_datoteku();
void ispis_podataka_o_svim_studentima();
void ispis_podataka_o_zadanom_studentu(int mb);
int main()
{
int izbor,mbr;
cout << "Unesite naziv datoteke u koju cete spremati podatke:";
cin >> naziv_datoteke;
do{
cout <<"Izaberite :" <<endl<<endl;
cout <<"1. Kreiranje datoteke"<<endl;
cout <<"2. Unos podataka u datoteku"<<endl;
cout <<"3. Ispis svih slogova iz datoteke"<<endl;
cout <<"4. Ispis podataka o zadanom studentu"<<endl;
cout <<"9. Izlaz iz programa"<<endl;
cin >> izbor;
switch (izbor){
case 1:kreiranje_datoteke();break;
case 2:unos_podataka_u_datoteku();break;
case 3:ispis_podataka_o_svim_studentima();break;
case 4:
cout << "Maticni broj studenta:";
cin >> mbr;
ispis_podataka_o_zadanom_studentu(mbr);
break;
}
} while (izbor != 9);
}
void kreiranje_datoteke(){
char promjena;
dat.open(naziv_datoteke,ios::in | ios::binary);
if (!dat){
dat.open(naziv_datoteke,ios::out | ios::binary);
dat.close();
}
else
{
cout << "Datoteka " << naziv_datoteke << " vec postoji na disku." << endl;
cout << "Ako ostane isti naziv datoteke, sadrzaj ce biti izbrisan."<<endl;
cout << "Hocete li promijeniti naziv datoteke? (d/n) "<<endl;
cin >> promjena;
if (promjena=='n') return;
cout << "Unesite naziv datoteke u koju cete spremati podatke:";
cin >> naziv_datoteke;
dat.open(naziv_datoteke,ios::out | ios::binary);
dat.close();
};
};
void unos_podataka_u_datoteku(){
char dalje;
dat.open(naziv_datoteke,ios::app | ios::binary);
do{
cout << "Maticni broj:";
cin >> student.mat_br;
cout << "Prezime i ime:";
cin >> student.prez_ime;
cout << "Godina studija:";
cin >> student.g_stu;
dat.write((char *) &student,sizeof(student));
cout << "Dalje? (d/n)";
cin >> dalje;
} while (dalje=='d');
dat.close();
};
void ispis_podataka_o_svim_studentima(){
dat.open(naziv_datoteke,ios::in | ios::binary);
while (1){
dat.read((char *) &student, sizeof(student));
if (dat.eof()) break;
cout << "Maticni broj: " << student.mat_br << endl;
cout << "Prezime i ime: " << student.prez_ime << endl;
cout << "Godina studija: " << student.g_stu << endl;
}
dat.close();
};
void ispis_podataka_o_zadanom_studentu(int mb){
int brojac=0;
dat.open(naziv_datoteke,ios::in | ios::binary);
while (dat.eof() == 0){
dat.read((char *) &student, sizeof(student));
if (student.mat_br==mb){
cout << "Maticni broj: " << student.mat_br << endl;
cout << "Prezime i ime: " << student.prez_ime << endl;
cout << "Godina studija: " << student.g_stu << endl;
brojac++;
}
}
if (brojac==0)
cout << "Student s maticnim brojem " << mb << " nije nadjen u datoteci."<<endl;
dat.close();
};