Habsburg

PRgo2VJ6Mog1

Nov 26th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include "biblioteka_vrijeme_i_binarno_pretrazivanje.cc"
  5. struct Abcd {
  6.     int kljuc;
  7.     float randomBroj;
  8. };//struct
  9. tindeks indeks;
  10.  
  11.  
  12. void mog1() {
  13.     fstream ind, dat;
  14.     dat.open("datoteka.dat", fstream::binary | fstream::in);
  15.  
  16.     if(!dat) {
  17.         cout << "status 0 = nema datoteke!";
  18.         cout << "Kreiram datoteku.... Datoteka kreirana!\n";
  19.         dat.open("datoteka.dat", fstream::binary | fstream::out);
  20.         ind.open("datoteka.ind", fstream::binary | fstream::out);
  21.         dat.close();
  22.         dat.clear();
  23.         ind.close();
  24.         ind.clear();
  25.         return;
  26.     }
  27.  
  28.     if( dat.peek() == fstream::traits_type::eof()) {
  29.         cout << "status 1 = datoteka je prazna!\n";
  30.     } else {
  31.         streampos fsize = 0;
  32.         dat.seekg(0, fstream::end);
  33.         fsize = dat.tellg() - fsize;
  34.         cout << "status 2 = datoteka sadrzi " << fsize/sizeof(Abcd)<< " zapisa\n";
  35.     }
  36.  
  37.     dat.close();
  38.     dat.clear();
  39.     ind.close();
  40.     ind.clear();
  41.    
  42.     bool izbor;
  43.     cout << "Zelis li raditi s postojecim datotekama? (1: DA; 0: NE): ";
  44.     cin >> izbor;
  45.     if(!izbor) {
  46.         dat.open("datoteka.dat", fstream::binary | fstream::out);
  47.         ind.open("datoteka.ind", fstream::binary | fstream::out);
  48.     }
  49.    
  50.     dat.close();
  51.     dat.clear();
  52.     ind.close();
  53.     ind.clear();
  54.  
  55.     Abcd object;
  56.     cout << "Unesite kljuc: ";
  57.     cin >> object.kljuc;
  58.     cout << "Unesite random broj: ";
  59.     cin >> object.randomBroj;
  60.  
  61.     dat.open ("datoteka.dat", ios::out|ios::in|ios::binary);
  62.     ind.open ("datoteka.ind", ios::out|ios::in|ios::binary);
  63.     dat.seekp(0, fstream::end);
  64.     ind.seekp(0, fstream::end);
  65.    
  66.     dat.write((char *)&object,sizeof(Abcd));
  67.     indeks.kljuc = object.kljuc;
  68.     indeks.adresa = (int)dat.tellp()-sizeof(object);
  69.     ind.write((char *)&indeks,sizeof(tindeks));
  70.     dat.close();
  71.     dat.clear();
  72.     ind.close();
  73.     ind.clear();
  74.    
  75.     preslozi_indeks("datoteka.ind",indeks);
  76.     ind.open("datoteka.ind", ios::in|ios::binary);
  77.     while(1) {
  78.         ind.read ((char *)&indeks,sizeof(tindeks));
  79.         if (ind.eof())
  80.             break;
  81.         cout << "---------------------" << endl;
  82.         cout << "kljuc: " << indeks.kljuc << endl;
  83.         cout << "adresa: " << indeks.adresa << endl;
  84.     }
  85.     dat.close();
  86.     dat.clear();
  87.     ind.close();
  88.     ind.clear();
  89. }
  90. int main() {
  91.     int izbor;
  92.     while(1) {
  93.         cout << "Izbor: ";
  94.         cin >> izbor;
  95.         if(izbor == 1)
  96.             mog1();
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment