Writing a File Size Function

A Guide to Obtaining File Information in C and Other Languages

© Guy Lecky-Thompson

Tutorial Article designed to help with those wanting to obtain the file size in Win32, Unix etc. using C, C++, BASIC, VB or other programming languages.

Introduction

Writing a robust file size function is a good exercise for beginning programmers. It requires understanding of many of the basic operating system and programming language fundamentals and there are many ways that it can be achieved. We will be looking at 2 ways:

  1. The Windows (Win32) Way
  2. The Standard C IO Way

Windows has its own set of file handling functions, dating back to Win16 and Win32 API programming days. Recently, the implementations changed a little, and it became commonplace to use the Standard C IO functions (from stdio.h) as in Unix based operating systems. Nonetheless, the Windows API function calls are still recommended for compatibility reasons.

If the language or API does not have a File Size function, then the basic theory runs like this:

Refinements to this include being able to pick up the file size from any position, and restore it if necessary. This is useful when a file is open for reading and appending; something that the standard C IO routines implement but that other languages and operating systems might not.

The Windows Way

The Windows API (since WinNT / Win32) has a function to get the file size directly, for any file that has been opened using the CreateFile API function. CreateFile is necessary because it returns a HANDLE which can be used in other Windows file handling API functions.

The actual definition of GetFileSize is:

DWORD GetFileSize ( HANDLE hFile, LPDWORD lpHugeSize );

The HANDLE is self-explanatory, but lpHugeSize might need some explanation. In fact, a DWORD can only contain a file size of up to 32 bits (2^32 = 4GB). This is probably as large as anyone would need, but just in case it is not, Microsoft has allowed a second DWORD to be supplied to get the high order 32bits, thereby allowing for a much larger file size.

As long as the programmer is sure that the file size will not exceed 4GB, they can supply NULL as the lpHugeSize parameter, and will be returned the file size as a DWORD. Otherwise, they must supply a pointer to a DWORD variable to store the value in.

The Standard C IO Way

The stdio.h library provides several functions for getting information about files, but lacks one to actually return the size of a file. However, we can implement our own function MyGetFileSize to deal with it.

Since we can open a file in C using the flags "a+" for 'append and read', it makes sense to provide a file size function that works without destroying the current file position. We can append to the file, as well as read from it, and so it might grow over time, hence the necessity for a file size function, and the reason why we do not want to destroy the file pointer in the process.

We use fseek to move the pointer through the file. It takes the file handle, the offset, and the origin (0 = start, 1 = current position, 2 = the end) as parameters. Once we have repositioned the pointer to the end, we can use ftell to get the pointer location, from the beginning of the file. The function itself is reasonably simple:

long MyGetFileSize ( FILE * hFile )
{
long lCurPos, lEndPos;
// Check for valid file pointer
if ( hFile == NULL )
{
return -1;
}
// Store current position
lCurPos = ftell ( hFile );
// Move to the end and get position
fseek ( hFile, 0, 2 );
lEndPos = ftell ( hFile );
// Restore the file pointer
fseek ( hFile, lCurPos, 0 );
return lEndPos;
}

A simple call to MyGetFileSize, with the file handle, will return the file size.

Other Languages

Many other languages are likely to have their own File Size functions, but the above processes can be used if not. Windows programmers always have the API functions available to them, and other operating systems usually support file seeking.


The copyright of the article Writing a File Size Function in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Writing a File Size Function must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo