c++ - Compressing the binary file -


after deleting record file, there unused amount of memory in file once occupied deleted record. many of these deleted records in file can cause file grow large contain little useful data. there couple of ways avoid this:

  1. whenever new record inserted in file, inserted in first available space in file, not @ end. however, can lead inefficiency if search file first available space - nullify usefulness of indexing structure operation. maintain simple queue held locationinfile of records had been deleted. whenever insertion required, element retrieved (de-queued) queue , used location store new record in file. (don’t forget update indexing structure).
  2. after threshold of deletions reached (let’s use 5 deletions example), binary file compressed. means of valid records in file moved beginning of file (compressed beginning). consider following picture each square represents place record in file, each integer represents valid record in use , each ‘x’ represents deleted record: 1 x x 2 x 3 x x 4

after compression of file, like: 1 2 3 4 x x x x x

notice of valid records moved top of file. method require significant updates indexing structure.

**any idea how implement in code. maybe seek function can useful. **

     // program uses structure variable store record file.  #include <iostream>  #include <fstream>  using namespace std;  // array sizes const int ssn_size = 10, name_size = 51, addr_size = 51, phone_size = 14;  // declare structure record.  struct info {     char ssn[ssn_size];     char name[name_size];     int age;     char phone[phone_size];  };  int main() {     info person;   // hold info person     char again;    // hold y or n     // open file binary output.     fstream people("people.dat", ios::out | ios::binary);     {         // data person.         cout << "enter following data " << "person:\n";         cout << "ssn: ";         cin.getline(person.ssn, ssn_size);         cout << "name: ";         cin.getline(person.name, name_size);         cout << "age: ";         cin >> person.age;         cin.ignore(); // skip on remaining newline.         cout << "phone: ";         cin.getline(person.phone, phone_size);          // write contents of person structure file.         people.write(reinterpret_cast<char *>(&person), sizeof(person));          // determine whether user wants write record.         cout << "do want enter record? ";         cin >> again;         cin.ignore(); // skip on remaining newline.     } while (again == 'y' || again == 'y');      // close file.     people.close();      people.open("people.dat", ios::in | ios::binary);      if (!people) {         cout << "error opening file. program aborting.\n";         return 0;     }      cout << "here people in file:\n\n";     // read first record file.     people.read(reinterpret_cast<char *>(&person),         sizeof(person));     // while not @ end of file, display records.     while (!people.eof()) {         // display record.         cout << "name: ";         cout << person.name << endl;         cout << "age: ";         cout << person.age << endl;         cout << "address line 1: ";         cout << person.address1 << endl;         cout << "address line 2: ";         cout << person.address2 << endl;         cout << "phone: ";         cout << person.phone << endl;                // wait user press enter key.         cout << "\npress enter key see next record.\n";         enter code here`cin.get(again);              // read next record file.         people.read(reinterpret_cast<char *>(&person),         sizeof(person));      }            cout << "that's data in file!\n";         people.close();         return 0; } 

there many things need think about... need make sure have enough space @ target position (no overlapping)...

actually functionality of filesystem, why reinvent wheel?

e.g. on linux can create filesystem inside file , mount it. can create file each person , save in filesystem. way (almost) sure have no bugs , receive security updates.

if not on linux can use filesystem (ntfs, ext4...) c++ apis directly use file filesystem.

if don't idea, suggest re-create entire file , delete old file. of course re-create non-deleted entries.

last no leaset: if file small think don't need defragmentation @ all.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -