1. Technology

Creating the fragmented disk Contest 45 in CSharp

With source


For this contest I needed an output file created with 200 x 200 locations, each representing a sector on disk and holding a value in the range 0 to 255 plus spaces and dots.

I created a simple console app. that creates an instance of a gendisk object and that does all of the work.

That file contains the diskgen class. To use it just create a simple console app, add the diskgen.cs file to the project and this one line constructor. You'll end up with something like this.

using System;
using System.Text;
using gendisk;

namespace p45
    class Program
        static void Main(string[] args)
            diskgen d = new diskgen() ;

How It Works

The purpose of this is to populate a large array (200 x 200) with around 250 "files", each "file" is made up of between 5 and 300 sectors. Each location in the array is either empty (-1), unusable (-2) or a value 0-255. All sectors of "file" 45 will have a value 45.

I wanted the disk to be about 90% full so the method GenerateFiles() uses a lowerbound of total (40,000) / 10. The total starts at 40,000 and as each location is allocated the total is lowered. The built in C# Random class is used to generate "files" of length 5-300 sectors. For each sector in a "file", the method getfreelocation finds an unused location with a value of -1. This is initialized in the diskgen() constructor and the tracks 1-10 are also initialized here to a value -2.

As the files and sectors are generated, a count of the number of files is maintained and if 255 is reached then it stops. To keep the disk file small, the decision was taken to use 2 hex digits for each location (or .. or 2 spaces) so 255 is the upper limit.

Finally the WriteFile method loops through the array and outputs 2 characters for each "file" number sector, unused (..) or unusable (2x spaces ) .

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. How To Do Things in C#
  6. Creating the fragmented disk for Contest 45 in CSharp

©2014 About.com. All rights reserved.