Page 1 of 1

Microsoft : Working With Disc Files The most common disc file functions Rate Topic: -----

#1 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Posted 02 April 2010 - 01:18 AM

Microsoft : Working With Disc Files

Introduction

Before we take a look at what Microsoft Windows provides for us in the way of an API, there are a few important things that we need to consider with discs and disc files that most programmers either ignore or are totally unaware of.

Firstly, discs are slow devices when compared to the processor and it's memory. When you access a disc, the disc hardware has to move the disc heads to the cylinder which contains the data. That is known as the seek time and takes 5-10 milliseconds (during which time your processor(s) could have executed at least one million instructions). The head for the surface that contains the data needs to be selected. The disc controller then has to wait until the index point is reached, before it knows where to start counting in order to obtain the correct sector that contains the data. That is known as the latency time and could be up to one revolution of the disc. That depends upon the speed of your disc (5000, 7500, 10000 rpm), but again takes a significant amount of time (a 10000 rpm disc rotates once every 6 milliseconds!). The data then has to be transferred off disc and into memory.

A disc sector is normally 4096 bytes in size, and the more contiguous sectors that we can read into memory or write from memory, the less head movement and latency time we will encounter. Unfortunately, due to the way modern operating systems handle disc file allocation, the disc file you are reading or writing will not be contiguous, but fragmented. This means cylinder head movement and latency for most disc files.

The operating system does try to help by keeping a disc cache that provides a mechanism whereby disc sectors associated with file writes are not written immediately to disc, but are marked as dirty. You can perform multiple writes to a disc sector in cache without causing the overheads associated with disc access. At some point in time though, the operating system will have no choice to write the sector to disc.

Creating or opening a disc file

To create or open a disc file, we use the CreateFile function. The fifth parameter tells the operating system if we are creating a new file or opening an existing file. The function returns a file handle that can be used in other functions.

    HANDLE file_handle = CreateFile("abc.def", GENERIC_READ|GENERIC_WRITE,
                                    0, NULL, OPEN_EXISTING, 
                                    FILE_ATTRIBUTE_NORMAL, NULL);



The code snippet above opens an existing file called "abc.def" for reading and writing.

Reading and writing to a disc file

To read from and write to a disc file, we use the ReadFile and WriteFile functions.

    error_code = ReadFile(file_handle, buffer, sizeof(buffer), &bytes_read, NULL);
    if ( error_code != TRUE || bytes_read != sizeof(buffer) ) ...



    error_code = WriteFile(file_handle, buffer, sizeof(buffer), &bytes_written, NULL);
    if ( error_code != TRUE || bytes_read != sizeof(buffer) ) ...



The code snippets above read and write to a file respectively.

Closing a disc file

To close a disc file, we use the CloseHandle function. Calling this function guarantees that all data held in cache is written to the disc before it is closed (assuming that updates have been made to that area of the disc). The code snippet below shows an example:

    CloseHandle(file_handle);



Obtaining the file size

To obtain the size of a file, we can use the GetFileSize function. Two parameters are passed on this function call, the second being the address of a 32-bit integer that receives the upper part of the 64-bit file size for files that are over 4GB in size. If it is known the file can never be that size, then NULL can be used as the address. The code snippet below shows an example:

    DWORD file_size = GetFileSize(file_handle, NULL);



Setting the position in the file

To set the file at a specific position in the file, we can use the SetFilePointer function. An offset is specified relative to either the beginning of the file, the end of file or the current file position. It should be noted that the file position can be set to beyond the current end of file, in order to extend the file. The bytes between the current end of file and the position that is being set to are indeterminite. The code snippet below shows as an example, how to set the file pointer to offset 4096 from the beginning of the file:

    SetFilePosition(file_handle, 4096, NULL, FILE_BEGIN);



Conclusion

The functions provided above are the most commonly used functions that the API offers, but there are many more. Always remember to think about how and when you access data from disc files.

This post has been edited by Martyn.Rae: 02 April 2010 - 01:55 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1