Microsoft KB Archive/63874

MS-DOS: Keeping Data in Files Contiguous ID Number: Q63874

3.20 3.21 3.30 3.30a 4.00 4.01 MS-DOS

Question:

My application uses four data files, each of which may be as large as 34 MB. The names of these files will never change, but the sizes change from day to day, due to the nature of my application. These files reside on a partition that is reserved for my application only. The data files will be the only things on that partition.

With my data files being so large, it would help if the data in the files could remain contiguous. Unfortunately, when I rewrite the data to the files, the data does not remain contiguous.

How can I keep the data contiguous in each file?

Response:

Listed below is one way to keep the data contiguous in your files:

  Start with your data files being as large as they will ever become.   Format the partitions where the data files will reside.   Create four 34 MB files with the names of your data files. There are many different ways to do this. Two different methods are as follows:   Fill each file full of zeros or some other spacer until it is 34 MB in size.   Use the lseek C function to open a file that is 34 MB and then write 0 (zero) bytes to it. 

However you do it, you need to end up with four files that are 34 MB in length.   In your application, make sure that you write over the data files, rather than deleting them and then writing them to disk again. When your application begins writing data to the file, a file with the same name already exists on the disk, so the data will write over the existing data in the same cluster order as the original file. Since the partition had been formatted before the four large files had been created, when you create the four large files, the clusters within each file will be contiguous (with the exception of clusters that have been marked as bad). It will make no difference if the size of each file changes, as long as the size does not exceed the size of the original spacer file (again, in this case, it would be 34 MB). 