mardi 3 octobre 2023

Goal: compute the "size on disk"

 You may have noticed that files have two sizes. 

The "file size" and the "size on disk", which is always greater or equal than the file size. Storage media are divided into "clusters". 

A file can occupy many clusters, but a cluster can only be allocated to one single file. The "size on disk" corresponds to the total size of all the clusters allocated to that file. Given the cluster size of a disk and a file size, compute its size on disk. 

Example A disk has a cluster size of 512 bytes. A file has a size of 1500 bytes. 2 clusters (512*2 = 1024 bytes) would not be enough to store this file. 4 clusters (2048 bytes) would be too much, one of the cluster would be empty. The correct number of clusters is 3. The total size of these clusters is (512 * 3 = 1536 bytes), which is the size on disk of this file.


using System;
public class Program
{
public static void Main()
{
int clusterSize = 512;
int fileSize = 1500;
var toReturn = 0;
if(fileSize % clusterSize > 0){
int nbCluster = (int) (fileSize / clusterSize);
toReturn = (nbCluster + 1) * clusterSize;
}

                if(fileSize % clusterSize == 0){
toReturn = fileSize;
}
Console.WriteLine(toReturn);
}
}

Aucun commentaire:

Enregistrer un commentaire