Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include "biblioteka_vrijeme_i_binarno_pretrazivanje.cc"
- struct Abcd {
- int kljuc;
- float randomBroj;
- };//struct
- tindeks indeks;
- void mog1() {
- fstream ind, dat;
- dat.open("datoteka.dat", fstream::binary | fstream::in);
- if(!dat) {
- cout << "status 0 = nema datoteke!";
- cout << "Kreiram datoteku.... Datoteka kreirana!\n";
- dat.open("datoteka.dat", fstream::binary | fstream::out);
- ind.open("datoteka.ind", fstream::binary | fstream::out);
- dat.close();
- dat.clear();
- ind.close();
- ind.clear();
- return;
- }
- if( dat.peek() == fstream::traits_type::eof()) {
- cout << "status 1 = datoteka je prazna!\n";
- } else {
- streampos fsize = 0;
- dat.seekg(0, fstream::end);
- fsize = dat.tellg() - fsize;
- cout << "status 2 = datoteka sadrzi " << fsize/sizeof(Abcd)<< " zapisa\n";
- }
- dat.close();
- dat.clear();
- ind.close();
- ind.clear();
- bool izbor;
- cout << "Zelis li raditi s postojecim datotekama? (1: DA; 0: NE): ";
- cin >> izbor;
- if(!izbor) {
- dat.open("datoteka.dat", fstream::binary | fstream::out);
- ind.open("datoteka.ind", fstream::binary | fstream::out);
- }
- dat.close();
- dat.clear();
- ind.close();
- ind.clear();
- Abcd object;
- cout << "Unesite kljuc: ";
- cin >> object.kljuc;
- cout << "Unesite random broj: ";
- cin >> object.randomBroj;
- dat.open ("datoteka.dat", ios::out|ios::in|ios::binary);
- ind.open ("datoteka.ind", ios::out|ios::in|ios::binary);
- dat.seekp(0, fstream::end);
- ind.seekp(0, fstream::end);
- dat.write((char *)&object,sizeof(Abcd));
- indeks.kljuc = object.kljuc;
- indeks.adresa = (int)dat.tellp()-sizeof(object);
- ind.write((char *)&indeks,sizeof(tindeks));
- dat.close();
- dat.clear();
- ind.close();
- ind.clear();
- preslozi_indeks("datoteka.ind",indeks);
- ind.open("datoteka.ind", ios::in|ios::binary);
- while(1) {
- ind.read ((char *)&indeks,sizeof(tindeks));
- if (ind.eof())
- break;
- cout << "---------------------" << endl;
- cout << "kljuc: " << indeks.kljuc << endl;
- cout << "adresa: " << indeks.adresa << endl;
- }
- dat.close();
- dat.clear();
- ind.close();
- ind.clear();
- }
- int main() {
- int izbor;
- while(1) {
- cout << "Izbor: ";
- cin >> izbor;
- if(izbor == 1)
- mog1();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment