Advertisement
Guest User

Fragmentation

a guest
Jan 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. void printDataStats(const char* name)
  2. {
  3.     FILE* file = openFile(name, "rb");
  4.     int freeBlocks = freeBlocksCount(file);
  5.     int busyBlocks = BLOCKS_NUMBER - freeBlocks;
  6.     fclose(file);
  7.  
  8.     int freeSpace = freeBlocks*BLOCK_SIZE;
  9.     int allSpace = BLOCK_SIZE*BLOCKS_NUMBER;
  10.     int busySpace = allSpace-freeSpace;
  11.  
  12.     cout << "Free Blocks: " << freeBlocks << "/" << BLOCKS_NUMBER << "\n";
  13.     cout << "Occupied Blocks: " << busyBlocks << "/" << BLOCKS_NUMBER << "\n";
  14.     cout << "Results in: \n";
  15.     cout << ((double)freeSpace/(double)allSpace)*100.0 << "% of free space\n";
  16.     cout << ((double)busySpace/(double)allSpace)*100.0 << "% of occupied space\n";
  17.     cout << freeSpace << "/" << allSpace << " bytes are free\n";
  18.     cout << busySpace << "/" << allSpace << " bytes are occupied\n";
  19.  
  20.  
  21.     /* Can comment if not working first time */
  22.         int i;
  23.         uint16_t temp;
  24.         unsigned long int tempPos;
  25.         file = openFile(name, "rb");
  26.  
  27.         int unusedBytes = 0;
  28.         while(true)
  29.         {
  30.             tempPos = ftell(file);
  31.             fread((void*) &temp, sizeof(temp), 1, file);
  32.             if(feof(file))
  33.                 break;
  34.             //printf("%#08lx\t", tempPos);
  35.             int bytescount = 0;
  36.             if((temp & 32768) == 0)
  37.             {
  38.  
  39.                 bool go = true;
  40.                 char c = 'a';
  41.                 while(c != '\0' && go)
  42.                 {
  43.                     fread((void*) &c, sizeof(c), 1, file);
  44.                     bytescount++;
  45.                     if(bytescount == BLOCK_SIZE)
  46.                     {
  47.                         //cout << "Break by BLOCK_SIZE\n";
  48.                         go = false;
  49.                     }
  50.                 }
  51.                 unusedBytes += BLOCK_SIZE-bytescount;
  52.             }
  53.             fseek(file, -bytescount+BLOCK_SIZE, SEEK_CUR);
  54.         }
  55.         fclose(file);
  56.  
  57.         cout << unusedBytes << "/" << allSpace << " lost by fragmentation\n";
  58.         cout << (double)unusedBytes/(double)allSpace*100.0 << "% of disk space lost by fragmentation\n";
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement